zabbix 扩展脚本
#!/usr/bin/env python
#encoding:utf8
# desc: self-inspection
# args:
# reboot : reboot AP
# check : check AP syslog
# getApNum: return AP Number Online
# checkApOnline: ok: return ok; false: return ap ip : local apInfo 读取这个文件开始测试是否在线 import os
import sys
from paramikoHelper import myParamiko
from time import sleep
from telnetAP import rebTelnet
import threading # AP IP信息文件
apInfoFile='/opt/script/apInfo' # 全局线程锁
lock = threading.RLock() # 多线程计数器
class Counter:
count = 0 def __init__(self, count):
self.count = count def add(self, n = 1):
lock.acquire()
self.count = self.count + n
lock.release() def delete(self, n = 1):
lock.acquire()
self.count = self.count - n
lock.release() def get(self):
return self.count # 读取命令执行的结果
def getRunCommRes(cmd):
return os.popen(cmd).read().strip('\n').strip(' ') # 获取所有AP的IP地址列表
def getAllIP():
return open(apInfoFile).read().split('\n')[:-1] # 通用工具类
# 规定:
# 定义普通方法的时候,必须定义两个默认且两个参数 arg1 = "", arg2 = ""
# 定义被threadBox调用的target方法的时候,
# 要满足4个参数,且第一个参数为ip(threadBox定义的),后面如果没有指定参数,需要用默认参数 arg2 = "", arg3 = "", arg4 = ""
# 只有一个参数IP时: def oTarget(ip, arg2 = "", arg3 = "", arg4 = ""):
class CommonUtil: def __init__(self):
pass # 多线程执行
def threadBox(slef, myTarget, arg1 = "", arg2 = "", arg3 = ""):
threads = [] for ip in getAllIP():
t = threading.Thread(target=myTarget, args=(ip, arg1, arg2, arg3))
threads.append(t)
t.start() for t in threads:
t.join() # 获取设备在线时长
def uptime(self, arg1 = "", arg2 = ""):
print getRunCommRes("uptime | cut -d ',' -f1 | awk -F'up' '{print $2}'") # 获取设备网络配置
def netInfo(self, arg1 = "", arg2 = ""):
ip = getRunCommRes("ip a show eth0 | grep inet | grep eth0 | awk '{print $2}'")
gateway = getRunCommRes("ip route | grep default | awk '{print $3}'")
print ip,gateway # 获取设备联盟号
def getPortalId(self, arg1 = "", arg2 = ""):
print getRunCommRes('cat /usr/local/etc/chilli/wid') # 运行指定的脚本文件
def runScript(self, arg1, arg2 = ""):
print getRunCommRes('/opt/script/' + arg1) # ping Target
def pingTarget(self, ip, res, downCount, args4 = ""):
code = os.system('/bin/ping -c 1 -w 3 ' + ip + '>/dev/null')
if code != 0:
code1 = os.system('/bin/ping -c 1 -w 3 ' + ip + '>/dev/null')
if code1 != 0:
res.append(ip.split('.')[-1])
downCount.add() # 获取AP在线数
# :::ex251::: 判断是否还有192.168.188.251的IP地址可以ping通
# 如果可以ping通表示还有AP没有修改IP地址
# a= 表示一共多少台AP, d=表示有多少台掉线, ::后面的数字表示192.168.188.+数字表示AP的ip地址
#
def checkApOnline(self, arg1 = "", arg2 = ""):
# 检查接口是否正常
interfaceName = getRunCommRes('ip addr show| grep 192.168.188.254').split('scope global')[1].strip(' ').split(':')[0]
code = os.system("ip link show " + interfaceName + " | grep 'state UP' 1>/dev/null 2>&1 ") if code != 0:
if 'eth2' == interfaceName:
print "err;if=down:eth1,," + interfaceName
else:
print "err;if=down:" + interfaceName
else:
res = []
total = len(getAllIP())
downCount = Counter(0)
ms251 = "" # 检查是否还存在251 IP的AP信息
state251 = os.system('/bin/ping -c 1 -w 3 192.168.188.251 >/dev/null')
if state251 == 0:
ms251 = ':::ex251:::' self.threadBox(self.pingTarget, res, downCount) if len(res) == 0:
print ms251 + 'ok;' + str(total)
else:
print ms251 + 'err;a=' + str(total) + ';d=' + str(downCount.get()) + '::' + str(res) # reboot target
def rebootTarget(self, ip, resDict, args3 = "", args4 = ""):
try:
sshobj = myParamiko(ip, 'root', 'password@123')
resInfo = sshobj.run_cmd("reboot")
resDict[ip] = resInfo["stdin"].strip('\n')
sleep(1)
sshobj.close() except Exception, e:
resDict[ip] = "Error: " + str(e)
if 'Authentication failed' in str(e):
try:
rebTelnet(ip, 'admin', 'password')
resDict[ip] = "telRebootAp!" except Exception, e1:
resDict[ip] = "Error: " + str(e1) # 重启AP
def reboot(self):
resDict = {} self.threadBox(self.rebootTarget, resDict) print resDict # 执行前环境初始化
def initSysDev():
# 检查chilli程序是否异常退出
chilliNum = getRunCommRes('ps -aef | grep chilli| grep -v grep | wc -l')
cwatchNum = getRunCommRes('ps -aef | grep cwatch| grep -v grep | wc -l ') #if int(chilliNum) == 0 and int(cwatchNum) == 0:
if int(chilliNum) == 0 :
os.system('/usr/local/sbin/cwatch.sh &') # 根据网络方式配置IP地址,方便和AP通信
os.system('ip addr del 192.168.188.254/24 dev eth1 1>/dev/null 2>&1')
os.system('ip addr del 192.168.188.254/24 dev eth2 1>/dev/null 2>&1')
os.system('ip addr del 192.168.188.254/24 dev eth3 1>/dev/null 2>&1') code = os.system('ifconfig | grep ^br0 >/dev/null')
if code == 0:
os.system('ip addr add 192.168.188.254/24 dev br0 1>/dev/null 2>&1')
else:
code1 = os.system("ip link show eth1 | grep 'state UP' 1>/dev/null 2>&1 ")
if code1 == 0:
os.system("ifconfig eth1:1 192.168.188.254 up 1>/dev/null 2>&1")
else:
os.system("ifconfig eth2:1 192.168.188.254 up 1>/dev/null 2>&1") # 执行开始
def main():
initSysDev() ComUtil = CommonUtil() if len(sys.argv) == 1:
print "please add args!" elif len(sys.argv) == 2:
myFun = getattr(ComUtil, sys.argv[1])
myFun() elif len(sys.argv) == 3:
myFun = getattr(ComUtil, sys.argv[1])
myFun(sys.argv[2]) elif len(sys.argv) == 4:
myFun = getattr(ComUtil, sys.argv[1])
myFun(sys.argv[2], sys.argv[3]) else:
print "args Error!" if __name__ == "__main__":
main()
zabbix 扩展脚本的更多相关文章
- zabbix通过脚本发送短信
zabbix通过脚本发送短信 原则 和zabbix电子邮件是一样的,他们是action内部配置,司的api来完毕.当然网上有不少利用139邮箱来发的,这个事实上算调用email的一种,这里复述的是调用 ...
- zabbix配合脚本监控Kafka
简介: Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据.Kafka如下特性,受到诸多公司的青睐. 1.高吞吐量:即使是非常普通的硬件Kafka也可以支持 ...
- zabbix统一脚本监控方式
几周的zabbix使用之后几点心得,暂时记在这儿 简单命令监控,直接配置Userparameter参数,以应用来分类conf文件,将不同应用的配置写在不同的conf文件里,并将之放到统一的配置引入目录 ...
- Zabbix 添加脚本检测IP变化
监控环境 IP和HOSTNAME 有时会有变化.但目前是通过IP地址监控,不是DNS名,添加一个外部脚本,发现IP和HOSTNAME发生变化时告警. vim /usr/local/etc/zabbix ...
- zabbix 自定义脚本监控activemq
1. 编写获取activemq队列积压消息(check-amq.sh) #!/bin/bash QUEUENAME=$ MQ_IP='172.16.1.56' curl -uadmin:admin h ...
- zabbix邮件脚本报警
#启动邮箱服务 systemctl start postfix.service #配置用户的邮箱发送邮件 vim /etc/mail.rc set from="xxx@xxx.com&quo ...
- zabbix自定义脚本监控服务器端口状态
zabbix可以通过客户端的[net.tcp.port[<ip>,port]]该item监控项来判断本地/远程服务器TCP端口是否正常,不过当时没有想起来,就用了自定义脚本去写的,很久没有 ...
- zabbix安装脚本
#!/bin/bash # #安装zabbix源.aliyun YUM源 #curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyu ...
- Zabbix告警脚本-短信
[root@iot-svndata02 bin]# cat zbsms.sh #!/bin/sh #curl http://221.179.180.137:8080/smsaServer/lkSend ...
随机推荐
- import datetime
import datetimenow = datetime.datetime.now()print('当前时间:',now) 当前时间: 2019-11-21 11:11:58.093122
- Tutorial: Create a Blinky ARM test project(创建一个闪灯的arm测试项目)
Background ref : Tutorial: Create a Blinky ARM test project If you are new to ARM development, it is ...
- Mybatis实现联合查询(六)
1. 疑问 在之前的章节中我们阐述了如何用Mybatis实现检查的查询,而我们实际的需求中,绝大部分查询都不只是针对单张数据表的简单查询,所以我们接下来要看一下Mybatis如何实现联合查询. 2. ...
- 洛谷 P2722 总分 Score Inflation && 完全背包模板
题目传送门 解题思路: 补一个完全背包的模板,跟01背包十分相似,唯一不同在于重量j的枚举顺序. AC代码: #include<cstdio> #include<iostream&g ...
- win10编译tensorflow C++接口
原文地址:https://www.bearoom.xyz/2018/08/28/win10-build-tf-cc/ 首先,我觉得这是一个比较DT的活,因为,tensorflow支持最好的编程语言应 ...
- 【@ConfigurationProperties注解】Not Found The requested URL /spring-boot/docs/2.2.2.RELEASE/reference/html/configuration-metadata.html was not found on this server.
<!-- 配置文件自动映射 --> <dependency> <groupId>org.springframework.boot</groupId> & ...
- PTA 自测-4 Have Fun with Numbers
#include<iostream> #include<string> #include<cstring> #include<vector> using ...
- 使用GitHub Pages服务进行域名URL转发
有时,你注册了一个域名,但是你没有搭建服务器.你希望这个域名能指向你的主页/博客/微博等.但是,很多域名注册商不提供这种服务,或者这是一项收费服务.这时你可以使用GitHub来实现这一功能. 你需要导 ...
- aop 实现原理
aop 底层采用代理机制实现 接口 + 实现类 :spring 采用 jdk 的 动态代理 只有实现类:spring 采用 cglib 字节码增强 aop专业术语 1.target(目标) 需要被代理 ...
- JavaScript学习笔记 - 进阶篇(3)- 流程控制语句
if语句 if语句是基于条件成立才执行相应代码时使用的语句. 语法: if(条件) { 条件成立时执行代码} 注意:if小写,大写字母(IF)会出错! 假设你应聘web前端技术开发岗位,如果你会HTM ...