#!/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 扩展脚本的更多相关文章

  1. zabbix通过脚本发送短信

    zabbix通过脚本发送短信 原则 和zabbix电子邮件是一样的,他们是action内部配置,司的api来完毕.当然网上有不少利用139邮箱来发的,这个事实上算调用email的一种,这里复述的是调用 ...

  2. zabbix配合脚本监控Kafka

    简介: Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据.Kafka如下特性,受到诸多公司的青睐. 1.高吞吐量:即使是非常普通的硬件Kafka也可以支持 ...

  3. zabbix统一脚本监控方式

    几周的zabbix使用之后几点心得,暂时记在这儿 简单命令监控,直接配置Userparameter参数,以应用来分类conf文件,将不同应用的配置写在不同的conf文件里,并将之放到统一的配置引入目录 ...

  4. Zabbix 添加脚本检测IP变化

    监控环境 IP和HOSTNAME 有时会有变化.但目前是通过IP地址监控,不是DNS名,添加一个外部脚本,发现IP和HOSTNAME发生变化时告警. vim /usr/local/etc/zabbix ...

  5. zabbix 自定义脚本监控activemq

    1. 编写获取activemq队列积压消息(check-amq.sh) #!/bin/bash QUEUENAME=$ MQ_IP='172.16.1.56' curl -uadmin:admin h ...

  6. zabbix邮件脚本报警

    #启动邮箱服务 systemctl start postfix.service #配置用户的邮箱发送邮件 vim /etc/mail.rc set from="xxx@xxx.com&quo ...

  7. zabbix自定义脚本监控服务器端口状态

    zabbix可以通过客户端的[net.tcp.port[<ip>,port]]该item监控项来判断本地/远程服务器TCP端口是否正常,不过当时没有想起来,就用了自定义脚本去写的,很久没有 ...

  8. zabbix安装脚本

    #!/bin/bash # #安装zabbix源.aliyun YUM源 #curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyu ...

  9. Zabbix告警脚本-短信

    [root@iot-svndata02 bin]# cat zbsms.sh #!/bin/sh #curl http://221.179.180.137:8080/smsaServer/lkSend ...

随机推荐

  1. 吴裕雄--天生自然 PHP开发学习:MySQL 读取数据

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  2. mongodb的常见使用命令行

    由于cms工程要连接mongodb所以需要在在cms服务端工程添加如下依赖:项目使用spring data mongodb操作mongodb数据库 <dependency> <gro ...

  3. [AC自动机]玄武密码

    题目描述 一个长度为\(N\)的母串,有四个元素分别是:N,S,W,N. 有M个长度为100的模式串. 现在要求每个模式串的前缀与母串匹配最长长度. 输入样例 7 3 SNNSSNS NNSS NNN ...

  4. C语言实现整数转字符串

    #include <stdio.h> void intToString(int N,char arr[]){ //仅支持有符号4字节的int类型,范围-2147483648 - 21474 ...

  5. 四十二、LAMP与LNMP web架构深度优化实战-第一部

    1.nginx.conf配置文件基本参数优化 1.1 隐藏nginx header内版本号信息 一些特定的系统及服务漏洞一般都和特定的软件版本号有关,我们应尽量隐藏服务器的敏感信息(软件名称及版本等信 ...

  6. 论文翻译——Recursive Deep Models for Semantic Compositionality Over a Sentiment Treebank

    Abstract Semantic word spaces have been very useful but cannot express the meaning of longer phrases ...

  7. Windows 常用配置 - 启用长路径

    Windows 启用长路径支持 打开注册表编辑器:regedit 找到如下路径:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSyte ...

  8. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第七天】(redis缓存)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  9. Java BigInteger详解

    BigInteger概述 可用于无限大的整数计算 所在的包 java.math.BigInteger; 构造函数 public BigInteger(String val) 成员函数 比较大小函数 p ...

  10. 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)

    2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) A 题意:有 n 个时刻 ...