zabbix -- 一键安装2.4.7客户端 (附:邮件微信警报)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix -- 一键安装2.4.7客户端 (附:邮件微信警报)相关的知识,希望对你有一定的参考价值。
小Q:各位,最近一直在玩zabbix,写点总结。
1、搭建安装脚本,一键安装2.4.7 客户端
2、报警脚本(微信 邮件)
----------------------------------------------------------------------------------------------
1、搭建安装脚本,一键安装2.4.7 客户端
a、首先要找一台原始服务器,放置zabbix_2.4.7安装包,和一个已经写好适合自己的配置文件。
b、脚本如下
#!/bin/bash function install_zabbix { mkdir /downloads cd /downloads wget http://121.41.73.56/zabbix-2.4.7.tar.gz ##配置一个服务器端的zabbix安装包 tar zxf zabbix-2.4.7.tar.gz cd /downloads/zabbix-2.4.7 ./configure --prefix=/usr --sysconfdir=/etc/zabbix --enable-agent make make install cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/ sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/#g" /etc/init.d/zabbix_agentd groupadd zabbix -g 201 useradd -g zabbix -u 201 -m zabbix mkdir /var/log/zabbix mkdir /var/run/zabbix chown zabbix.zabbix /var/log/zabbix chown zabbix:zabbix /var/run/zabbix chmod 755 /etc/init.d/zabbix_agentd cd /etc/zabbix mv /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf.bak wget ##自定义配置文件并配置在某服务器 #cd /usr/lib64 #ln -s /usr/local/lib/libiconv.so.2 ./ #ldconfig /etc/init.d/zabbix_agentd start } 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 -uroot ping|grep -c alive UserParameter=mysql.version,mysql -V | cut -f6 -d " "| sed ‘s/,//‘ UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep vda | head -1 | awk ‘{print $$6}‘ UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep vda | head -1 | awk ‘{print $$10}‘
2、报警脚本(微信 邮件)
-----------------------------------------------------------放置地址在配置文件中定义
--------------------------------------------------------------------weixin
#!/usr/local/php/bin/php <?php error_reporting(E_ALL); ini_set("display_errors", true); class SdkQywx { const CORPID = ‘wx1b71f3b1c380ce02‘; const CORP_SECRET = ‘S8-ZDuwiPipX0YQWs_xF9c4P6drWw08Z1MV0e_TgReTtYpn-4dqhBglthztHdFio‘; public static $_app_list = array( // ‘gonggaotongzhi‘ => 34, // ‘kaoqinxitong‘ => 35, // ‘huiyixitong‘ => 36, ‘om_monitor‘ => 43, ); const IMAGE = ‘image‘; const VOICE = ‘voice‘; const VIDEO = ‘video‘; const FILE = ‘file‘; public static $tmpConnections; private static $_access_token; private static $expire_on = 0; private $access_token; //阻止外部实例化,仅允许使用getInstance进行单例操作 private function __construct($access_token) { $this->access_token = $access_token; } //防止对象被复制 public function __clone() { trigger_error(‘Clone is not allowed !‘); } /** * 获取单例 * * @return <Object> */ public static function getInstance() { $access_token = self::fetchAccessToken(); if (!isset(self::$tmpConnections[$access_token]) || !(self::$tmpConnections[$access_token] instanceof self)) { // 清空连接池 self::$tmpConnections = array(); // 缓存新连接 self::$tmpConnections[$access_token] = new self($access_token); } return self::$tmpConnections[$access_token]; } private static function fetchAccessToken() { // 检查连接令牌是否超时 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‘])) { return null; } // 缓存令牌 self::$expire_on = time() + $result[‘expires_in‘]; self::$_access_token = $result[‘access_token‘]; } return self::$_access_token; } public function sendNewsMessage($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 ($content as $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 . ‘}}‘; return self::doPost($url, $params, $post_data); } private static function doGet($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); return json_decode($result, true); } private static function doPost($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); return json_decode($info, true); } } $receiver = "$argv[1]"; $title = "$argv[2]"; $desc = "$argv[3]"; $url = "http://zabbix.ledu.com/zabbix"; //$receiver = "zhuangzhenhua|fengkaiwen|caofeng|zhangxinyu"; $content = array( array( ‘title‘ => $title, ‘description‘ => $desc, ‘url‘ => $url ) ); $r = SdkQywx::getInstance()->sendNewsMessage(43, $receiver, $content);
----------------------------------------------------------------------------------python
#!/usr/bin/python #coding:utf-8 import smtplib from email.MIMEText import MIMEText import sys import os import argparse import logging import datetime mail_host = ‘mailex.ledu.com‘ mail_user = ‘[email protected]‘ mail_pass = ‘wslmwps‘ mail_postfix = ‘mailex.ledu.com‘ mail_nickname = ‘zabbix‘ ###################################### ###################################### global sendstatus global senderr def send_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 ‘send ok‘ sendstatus = True except Exception , e: sendert=str(e) #print senderr sendstatus = False def logwrite(sendstatus,mail_to,content): logpath=‘/var/log/zabbix/alert‘ if not sendstatus: content = senderr if not os.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)+‘mail send to{0},content is :\n {1}‘.format(mail_to,content)) if __name__=="__main__": parser = argparse.ArgumentParser(description=‘Send mail to user for zabbix alterting‘) parser.add_argument(‘mail_to‘,action="store",help=‘The address of the E-mail that send to user‘) parser.add_argument(‘subject‘,action="store",help=‘The subject of the E-mail‘) parser.add_argument(‘content‘,action="store",help=‘The content of the E-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.7客户端 (附:邮件微信警报)的主要内容,如果未能解决你的问题,请参考以下文章
Saltstack批量安装部署Zabbix代理(附zabbix自动注册详解)
使用shell脚本一键安装zabbix并配置客户端的自动发现选项
ansible-playbook基于role的配置一键安装zabbix客户端以及拉取自定义监控脚本