ZabbixからSlackに通知を送る

Standard

関連かんれんリソース:
https://github.com/ericoc/zabbix-slack-alertscript
を使います。基本的きほんてきにはこちらのreadme通りに設定せっていします。

Slack側の設定
https://yemaosheng.slack.com/services/new/incoming-webhook
からincoming web hookを作成さくせいし、”Webhook URL”を確認かくにんしておきます。

テストURL:

curl -X POST --data-urlencode 'payload={"channel": "#alert", "username": "webhookbot", "text": "This is posted to #alert and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/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

Percona Monitoring Plugins for Zabbix3

Standard
#Install
apt-get install percona-zabbix-templates
cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.conf.d/
#Configure 
vi /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
...
$mysql_user = 'uid';
$mysql_pass = 'pwd';
...
#Test
/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg

zbx_percona_mysql_template

AWS Cloudwatch query script for Zabbix

Standard
#!/usr/bin/python
import boto.ec2.cloudwatch
import sys
import datetime
 
try:
    metName = sys.argv[1]
    funcName = sys.argv[2]
    dimSpace = sys.argv[3]
    region = sys.argv[4]
    accessKey = sys.argv[5]
    secretKey = sys.argv[6]
 
except:
    print "Usage: GetFromCloudwatch.py MetricName Function Dimension Region AWS_ACCESS_KEY AWS_SECRET_ACCESS_KEY"
    print "Example: GetFromCloudwatch.py FreeableMemory Average \"CacheClusterId=ElsticCacheName\" us-east-1 ACCESS_KEY SECRET_ACCESS_KEY"
    print "         GetFromCloudwatch.py CPUUtilization Average \"DBInstanceIdentifier=RDSName\" us-east-1 ACCESS_KEY SECRET_ACCESS_KEY"
    print "         GetFromCloudwatch.py ApproximateNumberOfMessagesVisible Average \"QueueName=SQSName\" us-east-1 ACCESS_KEY SECRET_ACCESS_KEY"
    sys.exit(1)
 
dim = {}
firstSplit = dimSpace.split(',')
for word in firstSplit:
    secondSplit = word.split('=')
    dim[secondSplit[0]] = secondSplit[1]
 
regions = boto.ec2.cloudwatch.regions()
 
reg = ''
for r in regions:
    if region == r.name:
        reg = r
 
c = boto.ec2.cloudwatch.CloudWatchConnection(aws_access_key_id=accessKey, aws_secret_access_key=secretKey, region=reg)
metrics = c.list_metrics(dimensions=dim)
 
end = datetime.datetime.utcnow()
start = end - datetime.timedelta(minutes=15)
 
dataPoints = [];
for met in metrics:
    if met.name == metName:
        dataPoints = met.query(start, end, funcName)
 
if len(dataPoints) > 0:
    max = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
    index = 0
    for i in range(0,len(dataPoints)):
        if max < dataPoints[i][u'Timestamp']:
            max = dataPoints[i][u'Timestamp']
            index = i
    for key in dataPoints[index].keys():
        if funcName in key:
            value = dataPoints[index][key]
    print value
else:
    print 'Error! No response from Amazon.'
    sys.exit(2)

SMS and phone call from Zabbix using Twilio

Standard


vim /etc/zabbix/alert.d/zabbix-alert-sms-twilio.sh

#!/usr/bin/python
import sys
from twilio.rest import TwilioRestClient
 
# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "AC0axxxxxxxxxxxxxxxxxxxx296ae"
auth_token  = "93e4xxxxxxxxxxxxxxxxxxx63e9"
client = TwilioRestClient(account_sid, auth_token)
 
message = client.messages.create(body=sys.argv[2],
    to=sys.argv[1],
    from_="+16572338xx8") # Replace with your Twilio number
print message.sid

vim /etc/zabbix/alert.d/zabbix-alert-call-twilio.sh

#!/usr/bin/python
import sys
import urllib
from twilio.rest import TwilioRestClient
 
# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "AC0axxxxxxxxxxxxxxxxxxxx296ae"
auth_token  = "93e4xxxxxxxxxxxxxxxxxxx63e9"
client = TwilioRestClient(account_sid, auth_token)
 
TwiML="http://yemaosheng.com/getTwiML.php?say="+urllib.quote(sys.argv[2])
 
call = client.calls.create(to=sys.argv[1],
    from_="+16572338xx8",
    url=TwiML)
print call.sid

vim /var/www/html/getTwiML.php

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say voice="woman"><?php echo htmlspecialchars($_GET['say']); ?></Say>
    <Hangup/>
</Response>

gentoo上面的nagios + nginx配置

Standard
#基础支持 gcc glibc glibc-common gd gd-devel
useradd -m nagios #添加一个名为nagios的用户用以专门跑nagios           
passwd nagios   #设置密码
groupadd nagcmd #添加nagcmd用户组,用以通过web页面提交外部控制命令
usermod -a -G nagcmd nagios #将nagios用户加入nagcmd组
usermod -a -G nagcmd nginx #将nginx用户加入nagcmd组
 
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.0.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
wget http://nagios.manubulon.com/nagios-snmp-plugins.1.1.1.tgz
tar zxvf nagios-3.2.0.tar.gz
tar zxvf nagios-plugins-1.4.13.tar.gz
tar zxvf nagios-snmp-plugins.1.1.1.tgz
 
cd nagios-3.2.0
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
这时nagios本身已基本安装完成
vi /usr/local/nagios/etc/objects/contacts.cfg #修改nagiosadmin这行其中的邮件地址为你的email地址,以将报警邮件发到你的邮箱
#make install-webconf #安装nagios的web接口
#htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
#设置登陆web界面时HTTP验证的账号密码
#因为我是用nginx,所以上面三行不执行,自己手工改nginx配置
 
cd ..
cd nagios-plugins-1.4.13
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
 
cd ..
cd nagios_plugins
#安装perl所用组件
perl -MCPAN -e shell
cpan> install Net::SNMP
./install.sh #一路回车
 
#nagios全套安装完毕
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg #检测nagios的配置是否正确
 
/etc/init.d/nagios start
/etc/init.d/nginx start

nagios要用cgi-bin,关于nginx上的cgi-bin配置可参考 nginx配置cgi-bin运行perl

转载请注明出处: http://yemaosheng.com

nginx配置cgi-bin运行perl

Standard

系统装的是Gentoo
实际上在gentoo上面如果用apache的话那装nagios是相当方便的,但我就是想把nagios装在nginx上面没办法^_^

wget http://www.nginx.eu/nginx-fcgi/nginx-fcgi.txt
mv nginx-fcgi.txt nginx-fcgi.pl
mv nginx-fcgi.pl /usr/bin/
chmod 777 /usr/bin/nginx-fcgi.pl
 
perl -MCPAN -e shell
cpan> install FCGI
cpan> install IO::All
cpan> install Socket
 
nginx-fcgi.pl -l /tmp/nginx-fcgi.log -pid /tmp/nginx-fcgi.pid -S /tmp/nginx-fcgi.sock
chmod 777 /tmp/nginx-fcgi.*
 
vi /etc/nginx/vhosts/nagios.conf
server
{
  listen       80;
  server_name  nagios.yemaosheng.com;
  index index.php index.html index.htm;
  root  /usr/local/nagios/share;
  location ~ .*\.php?$
  {
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /usr/local/nagios/share$fastcgi_script_name;
  }
  location /nagios/
  {
    gzip off;
    alias /usr/local/nagios/share/;
    index index.html index.htm index.php;
  }
  location ~ \.cgi$ {
     rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
     fastcgi_pass unix:/tmp/nginx-fcgi.sock;
     fastcgi_index index.cgi;
     fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
     fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;
     include fastcgi_params;
  }
  location ~ \.pl$ {
     fastcgi_pass  unix:/tmp/nginx-fcgi.sock;
     fastcgi_index index.pl;
     fastcgi_param SCRIPT_FILENAME  /usr/local/nagios/sbin$fastcgi_script_name;
     include /etc/nginx/fastcgi_params;
  }
}

转载请注明出处: http://yemaosheng.com

cacti备份及恢复shell

Standard

在网上找到下面两个shell,先收着.
备份Cacti
(1)cacti的文件目录的备份(rra目录除外)
(2)mysql备份
mysqldump -p cacti >/tmp/cacti.sql
(3)备份rra
dumprrd.sh

#!/bin/sh
rrddump="/usr/local/bin/rrdtool dump"
xmldir="/tmp/mysqlbackup/rraxml"
xmlbackup="/tmp/mysqlbackup/rrabak"
mysqlbackupDIR="/tmp/mysqlbackup"
rrdpath="/usr/local/share/cacti/rra/"
DATE=`date +"%y-%m-%d"`
 
if [ ! -e ${xmldir} ];then
   mkdir ${xmldir}
fi
 
if [ ! -e ${xmlbackup} ];then
  mkdir ${xmlbackup}
fi
 
#create rra => xml
for file in `find /usr/local/share/cacti/rra/ -mtime -2 | awk -F\/ '{print $NF}'`
  do
    ${rrddump} ${rrdpath}${file} > ${xmldir}/${file}.xml
  done
 
if [  `ls /tmp/mysqlbackup/rraxml/|wc -l`  -ge 2 ];then
   #tar backup
   cd ${mysqlbackupDIR}
   tar czvf ${xmldir}.${DATE}.tar.gz rraxml >/dev/null 2>&1
   mv ${xmldir}.${DATE}.tar.gz ${xmlbackup}
 
#delete file&dir
  if [ $? -eq 0 ];then
    rm -fdr ${xmldir}
    mkdir ${xmldir}
    /usr/bin/find ${mysqlbackupDIR}/rrabak -type f -ctime +2 | /usr/bin/xargs /bin/rm >/dev/null 2>&1
    echo "Rra Backup is ok!"
  fi
else
   echo "Rra Backup is fail!"
fi

恢复Cacti
(1)cacti的文件目录的备份(rra目录除外)
(2)mysql恢复
mysql cacti

#!/bin/sh
rrdrestore="/usr/local/bin/rrdtool restore"
xmldir=/tmp/rraxml/
for file in `find ${xmldir} | awk -F\/ '{print $NF}' | sed s/.xml//g`
  do
     echo $file
    ${rrdrestore} ${xmldir}${file}.xml /usr/local/share/cacti/rra/${file}
  done
chown -R cacti:cacti /usr/local/share/cacti/rra