1. 云栖社区>
  2. PHP教程>
  3. 正文

zabbix -- 一键安装2.4.7客户端 (附:邮件、微信警报)

作者:用户 来源:互联网 时间:2017-12-01 13:23:23

安装客户端邮件zabbix2.4

zabbix -- 一键安装2.4.7客户端 (附:邮件、微信警报) - 摘要: 本文讲的是zabbix -- 一键安装2.4.7客户端 (附:邮件、微信警报), 小Q:各位,最近一直在玩zabbix,写点总结。 1、搭建安装脚本,一键安装2.4.7客户端 2、报警脚本(微信邮件) -----------------------------------------------------------


小Q:各位,最近一直在玩zabbix,写点总结。

1、搭建安装脚本,一键安装2.4.7客户端

2、报警脚本(微信邮件)

----------------------------------------------------------------------------------------------

1、搭建安装脚本,一键安装2.4.7客户端

a、首先要找一台原始服务器,放置zabbix_2.4.7安装包,和一个已经写好适合自己的配置文件。

b、脚本如下

#!/bin/bash
functioninstall_zabbix
{
mkdir/downloads
cd/downloads
wgethttp://ip/zabbix-2.4.7.tar.gz
##配置一个服务器端的zabbix安装包
tarzxfzabbix-2.4.7.tar.gz
cd/downloads/zabbix-2.4.7
./configure--prefix=/usr--sysconfdir=/etc/zabbix--enable-agent
make
makeinstall
cpmisc/init.d/fedora/core/zabbix_agentd/etc/init.d/
sed-i"s#BASEDIR=/usr/local#BASEDIR=/usr/#g"/etc/init.d/zabbix_agentd
groupaddzabbix-g201
useradd-gzabbix-u201-mzabbix
mkdir/var/log/zabbix
mkdir/var/run/zabbix
chownzabbix.zabbix/var/log/zabbix
chownzabbix:zabbix/var/run/zabbix
chmod755/etc/init.d/zabbix_agentd
cd/etc/zabbix
mv/etc/zabbix/zabbix_agentd.conf/etc/zabbix/zabbix_agentd.conf.bak
wgethttp://IP/zabbix_agentd.conf
##自定义配置文件并配置在某服务器
#cd/usr/lib64
#ln-s/usr/local/lib/libiconv.so.2./
#ldconfig
/etc/init.d/zabbix_agentdstart
}
install_zabbix

c、zabbix_agentd.conf文件如下

LogFile=/var/log/zabbix/zabbix_agentd.log
PidFile=/var/run/zabbix/zabbix_agentd.pid
EnableRemoteCommands=0
Server=127.0.0.1,10.47.67.163
StartAgents=8
Hostname=clientname
ServerActive=10.47.67.163:10051
Timeout=30
Include=/etc/zabbix/zabbix_agentd.conf.d/
UnsafeUserParameters=1
UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin-urootping|grep-calive
UserParameter=mysql.version,mysql-V|cut-f6-d""|sed's/,//'
UserParameter=custom.vfs.dev.read.sectors[*],cat/proc/diskstats|grepvda|head-1|awk'{print$$6}'
UserParameter=custom.vfs.dev.write.sectors[*],cat/proc/diskstats|grepvda|head-1|awk'{print$$10}'

2、报警脚本(微信邮件)

-----------------------------------------------------------放置地址在配置文件中定义

--------------------------------------------------------------------weixin

#!/usr/local/php/bin/php
<?php
error_reporting(E_ALL);
ini_set("display_errors",true);
classSdkQywx
{
constCORPID='wx1b71f3b1c380ce02';
constCORP_SECRET='S8-ZDuwiPipX0YQWs_xF9c4P6drWw08Z1MV0e_TgReTtYpn-4dqhBglthztHdFio';
publicstatic$_app_list=array(
//'gonggaotongzhi'=>34,
//'kaoqinxitong'=>35,
//'huiyixitong'=>36,
'm_monitor'=>43,
);
constIMAGE='image';
constVOICE='voice';
constVIDEO='video';
constFILE='file';
publicstatic$tmpConnections;
privatestatic$_access_token;
privatestatic$expire_on=0;
private$access_token;
//阻止外部实例化,仅允许使用getInstance进行单例操作
privatefunction__construct($access_token)
{
$this->access_token=$access_token;
}
//防止对象被复制
publicfunction__clone()
{
trigger_error('Cloneisnotallowed!');
}
/**
*获取单例
*
*@return<Object>
*/
publicstaticfunctiongetInstance()
{
$access_token=self::fetchAccessToken();
if(!isset(self::$tmpConnections[$access_token])||!(self::$tmpConnections[$access_token]instanceofself))
{
//清空连接池
self::$tmpConnections=array();
//缓存新连接
self::$tmpConnections[$access_token]=newself($access_token);
}
returnself::$tmpConnections[$access_token];
}
privatestaticfunctionfetchAccessToken()
{
//检查连接令牌是否超时
if(self::$expire_on<=time())
{
//调用接口获取连接令牌
$result=self::doGet('https://qyapi.weixin.qq.com/cgi-bin/gettoken',array(
'corpid'=>self::CORPID,
'corpsecret'=>self::CORP_SECRET,
));
//校验结果
if(isset($result['errcode']))
{
returnnull;
}
//缓存令牌
self::$expire_on=time()+$result['expires_in'];
self::$_access_token=$result['access_token'];
}
returnself::$_access_token;
}
publicfunctionsendNewsMessage($agentid,$user_id,$content)
{
$url='https://qyapi.weixin.qq.com/cgi-bin/message/send';
$params=array('access_token'=>$this->access_token);
if(is_array($user_id))
{
$user_id=implode('|',$user_id);
}
$agentid=(int)$agentid;
$articles='[';
$tips='';
foreach($contentas$article)
{
$article['title']=str_replace('"','/"',$article['title']);
$article['description']=str_replace('"','/"',$article['description']);
$article['url']=str_replace('"','/"',$article['url']);
$articles.=$tips.'{"title":"'.$article['title'].'","description":"'.$article['description'].'","url":"'.$article['url'].'"}';
$tips=',';
}
$articles.=']';
$post_data='{"touser":"'.$user_id.'","msgtype":"news","agentid":'.$agentid.',"news":{"articles":'.$articles.'}}';
returnself::doPost($url,$params,$post_data);
}
privatestaticfunctiondoGet($url,$params=array())
{
$ch=curl_init($url.'?'.http_build_query($params));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//获取数据返回
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);//在启用CURLOPT_RETURNTRANSFER时候将获取数据返回
$result=curl_exec($ch);
curl_close($ch);
returnjson_decode($result,true);
}
privatestaticfunctiondoPost($url,$params=array(),$post_data=array())
{
$ch=curl_init($url.'?'.http_build_query($params));
curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$info=curl_exec($ch);
curl_close($ch);
returnjson_decode($info,true);
}
}
$receiver="$argv[1]";
$title="$argv[2]";
$desc="$argv[3]";
$url="http://zabbix网址/zabbix";
//$receiver="接收人";
$content=array(
array(
'title'=>$title,
'description'=>$desc,
'url'=>$url
)
);
$r=SdkQywx::getInstance()->sendNewsMessage(43,$receiver,$content);

----------------------------------------------------------------------------------python

#!/usr/bin/python
#coding:utf-8
importsmtplib
fromemail.MIMETextimportMIMEText
importsys
importos
importargparse
importlogging
importdatetime
mail_host='mailex.com'
mail_user='[email protected]'
mail_pass='wslmwps'
mail_postfix='mailex.com'
mail_nickname='zabbix'
######################################
######################################
globalsendstatus
globalsenderr
defsend_mail(mail_to,subject,content):
me=mail_nickname+"<"+mail_user+">"
msg=MIMEText(content)
msg['Subject']=subject
msg['From']=me
msg['to']=mail_to
try:
smtp=smtplib.SMTP()
smtp.connect(mail_host)
smtp.login(mail_user,mail_pass)
smtp.sendmail(me,mail_to,msg.as_string())
print'sendok'
sendstatus=True
exceptException,e:
sendert=str(e)
#printsenderr
sendstatus=False
deflogwrite(sendstatus,mail_to,content):
logpath='/var/log/zabbix/alert'

ifnotsendstatus:
content=senderr
ifnotos.path.isdir(logpath):
os.makedirs(logpath)
t=datetime.datetime.now()
daytime=t.strftime('%Y-%m-%d')
daylogfile=logpath+'/'+str(daytime)+'.log'
logging.basicConfig(filename=daylogfile,level=logging.DEBUG)
logging.info('*'*130)
logging.debug(str(t)+'mailsendto{0},contentis:/n{1}'.format(mail_to,content))
if__name__=="__main__":
parser=argparse.ArgumentParser(description='Sendmailtouserforzabbixalterting')
parser.add_argument('mail_to',action="store",help='TheaddressoftheE-mailthatsendtouser')
parser.add_argument('subject',action="store",help='ThesubjectoftheE-mail')
parser.add_argument('content',action="store",help='ThecontentoftheE-mail')
args=parser.parse_args()
mail_to=args.mail_to
subject=args.subject
content=args.content

send_mail(mail_to,subject,content)
#logwrite(sendstatus,mail_to,content)

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索安装 , 客户端 , 邮件 , zabbix , 2.4 , ,以便于您获取更多的相关知识。