做公司网站需要制作内容济宁网站开发公司
news/
2025/10/3 16:12:36/
文章来源:
做公司网站需要制作内容,济宁网站开发公司,个人网站可以做百度竞价,上海工商管理局官网有时候被ZABBIX监控的主机可能需要关机重启等维护操作,为了在此期间不触发告警,需要创建主机的维护任务,以免出现误告警
ZABBIX本身有这个API可供调用(不同版本细节略有不同,本次用的ZABBIX6.*),实现批量化建立主机的维护任务
无论哪种方式(IP列表,主机描述,或IP子网)创建维护…有时候被ZABBIX监控的主机可能需要关机重启等维护操作,为了在此期间不触发告警,需要创建主机的维护任务,以免出现误告警
ZABBIX本身有这个API可供调用(不同版本细节略有不同,本次用的ZABBIX6.*),实现批量化建立主机的维护任务
无论哪种方式(IP列表,主机描述,或IP子网)创建维护,都是向maintenance.create这个方法传递参数的过程,除了起始和终止的时间,最主要的一个参数就是hostids这个参数,它是一个列表(也可以是单个hostid) def create_host_maintenance(self,names,ips,starttimes,stoptimes):startsself.retimes(starttimes)stopsself.retimes(stoptimes)data json.dumps({jsonrpc:2.0,method:maintenance.create,params: {name: names,active_since: starts,active_till: stops,#hostids: [ 12345,54321 ],hostids: ips,timeperiods: [{timeperiod_type: 0,start_date: starts,#start_time: 0,zabbix6不用这个参数,低版本的有用period: int(int(stops)-int(starts))}]},auth: self.authID,id: 1,})request urllib2.Request(self.url, data)for key in self.header:request.add_header(key, self.header[key])try:result urllib2.urlopen(request)except URLError as e:print Error as , eelse:response json.loads(result.read())result.close()print maintenance is created!1.简单说明hostids这个列表参数产生的过程
按IP列表产生hostids的LIST,并调用方法创建维护
def djmaintenanceiplist(self,ipliststrstrs,areanone,byipnone):lists []if areanone: #通过IP列表来创建维护,只向函数传递ipliststr这个参数,其它参数为空for line in ipliststr.splitlines():con line.split()ipaddr con[0].strip()hostids self.host_getbyip(ipaddr)if hostids ! error and hostids not in lists:lists.append(hostids)return listselse:if area !ip and ipliststr ! strs: #按主机描述匹配创建维护,ipliststr参数因定为strs,area参数为主机描述,byip参数为空(因为网络环境的原因,本例弃用这个条件)sqlsselect hostid from zabbixdb.hosts where name like %area% testspysql.conn_mysql()datastests.query_mysqlrelists(sqls)if datas ! error:for ids, in datas:if ids not in lists:lists.append(ids)else:if byip ! none: #按主机IP子网创建维护,byip参数不为空(因为网络环境的原因,本例弃用这个条件)sqls select hosts.hostid from zabbixdb.hosts,zabbixdb.interface where hosts.hostidinterface.hostid and (interface.ip like % byip % or hosts.name like % byip %)tests pysql.conn_mysql()datas tests.query_mysqlrelists(sqls)if datas ! error:for ids, in datas:if ids not in lists:lists.append(ids)return listsdef djiplist(self,starttime,stoptime,strlins): #strlins为IP列表的参数,可由WEB前端传递进来test ZabbixTools()test.user_login()lists test.djmaintenanceiplist(strlins)nowtime str(time.strftime(%Y-%m-%d-%H-%M-%S, time.localtime(time.time())))test.create_host_maintenance(nowtime, lists, starttime, stoptime)按主机描述和IP子网产生hostids的LIST,并调用方法创建维护
def djdescript(self,starttime,stoptime,descstr,ipnetsnone):lists []if descstr ! ip: #descstr参数不为ip的时候,表示根据主机的描述信息匹配创建维护,此时不传递innets参数for line in descstr.splitlines():con line.split()descstrnew con[0].strip()sqls select hostid from dbname.hosts where name like % descstrnew % tests pysql.conn_mysql()datas tests.query_mysqlrelists(sqls)if datas ! error:for ids, in datas:if ids not in lists:lists.append(ids)else:if ipnets ! none: #ipnets参数不为空,表示按照IP子网匹配创建维护,此时descstr参数一定为ipsqls select hosts.hostid from dbname.hosts,dbname.interface where hosts.hostidinterface.hostid and (interface.ip like % ipnets % or hosts.name like % ipnets %)tests pysql.conn_mysql()datas tests.query_mysqlrelists(sqls)if datas ! error:for ids, in datas:if ids not in lists:lists.append(ids)test ZabbixTools()test.user_login()nowtime str(time.strftime(%Y-%m-%d-%H-%M-%S, time.localtime(time.time())))test.create_host_maintenance(nowtime, lists, starttime, stoptime) 2.create_host_maintenance和djmaintenanceiplist,及djdescript函数调用方法的说明
时间转换函数self.retimes,将用户传递/的%Y-%m-%d %H:%M:%S日期时间转换为时间时间戳 def retimes(self,times):timeArray time.strptime(times, %Y-%m-%d %H:%M:%S)timestamp time.mktime(timeArray)return timestamp
self.host_getbyip(ipaddr)通过主机IP获取zabbix的hostid,这个方法应用很广泛
函数中的self.authID通过zabbix的API 的user.login方法获取,参考ZABBIX官方文档 def host_getbyip(self,hostip):data json.dumps({jsonrpc:2.0,method:host.get,params:{output:[hostid,name,status,available],filter: {ip: hostip,custom_interfaces:0}},auth:self.authID,id:1,})request urllib2.Request(self.url, data)for key in self.header:request.add_header(key, self.header[key])try:result urllib2.urlopen(request)except URLError as e:if hasattr(e, reason):print We failed to reach a server.print Reason: , e.reasonelif hasattr(e, code):print The server could not fulfill the request.print Error code: , e.codeelse:response json.loads(result.read())result.close()lenslen(response[result])if lens 0:return response[result][0][hostid]else:print error hostipreturn error
pysql.conn_mysql()的实现
#coding:utf-8
import pymysql
class conn_mysql:def __init__(self):self.db_host zabbixdbipself.db_user dbusernameself.db_passwd dbuserpasswordself.database dbnamedef query_mysqlrelists(self, sql):conn pymysql.connect(hostself.db_host, userself.db_user, passwdself.db_passwd,databaseself.database)cur conn.cursor()cur.execute(sql)data cur.fetchall()cur.close()conn.close()#print data# 查询到有数据则进入行判断row有值且值有效则取指定行数据无值则默认第一行if data ! None and len(data) 0:return data#调用方式:for ids, in datas:else:return error#此方法返回的数据不含数据库字段的列表,如果要返回包含列名(KEY)的字典列表,则:conn pymysql.connect(hostself.db_host, userself.db_user, passwdself.db_passwd,databaseself.database,cursorclasspymysql.cursors.DictCursor) 解决:tuple indices must be integers, not str3.VIEWS.PY及前端和前端伟递参数的说明
views.py对应的方法
#login_required
def zabbixmanage(request):if request.methodPOST:uptime request.POST.get(uptime)downtime request.POST.get(downtime)UTC_FORMAT %Y-%m-%dT%H:%MutcTime datetime.datetime.strptime(uptime, UTC_FORMAT)uptime str(utcTime datetime.timedelta(hours0))utcTime datetime.datetime.strptime(downtime, UTC_FORMAT)downtime str(utcTime datetime.timedelta(hours0))if request.POST.has_key(iplists):try:sqlstrrequest.POST.get(sqlstr)u1upproxy.ZabbixTools() #前面的python文件名为upproxy.py,类名为ZabbixToolsu1.djiplist(uptime,downtime,sqlstr)except Exception:return render(request,zbx1.html,{login_err:FAILSTEP1})if request.POST.has_key(descs):try:sqlstr request.POST.get(sqlstr)u1 upproxy.ZabbixTools() u1.djdescript(uptime,downtime,sqlstr)except Exception:return render(request,zbx1.html,{login_err:FAILSTEP2})if request.POST.has_key(ipnets):try:sqlstr request.POST.get(sqlstr)u1 upproxy.ZabbixTools()u1.djdescript(uptime,downtime,ip,sqlstr)except Exception:return render(request,zbx1.html,{login_err:FAILSTEP3})HTML的简单写法,不太会写,很潦草
!DOCTYPE html
html langen
headmeta charsetutf-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleZABBIXMANAGE/title
/head
bodydiv idcontainer classcls-containerdiv idbg-overlay /divdiv classcls-header cls-header-lgdiv classcls-brandh3ZABBIX主机维护模式/h3h4开始时间小于结束时间/h4/div/divdiv classcls-contentdiv classcls-content-sm paneldiv classpanel-bodyform idloginForm action{% url zabbixmanage %} methodPOST {% csrf_token %}div classform-groupdiv classinput-groupdiv classinput-group-addoni classfa fa-user/i/divtextarea typetext classform-control namesqlstr placeholderIP列表,关键字或者IP网段 stylewidth:300px;height:111px/textarea/br/div/div/br/br开始时间:input typedatetime-local nameuptime/br/br结束时间:input typedatetime-local namedowntime/br/brp classpad-btm按IP列表维护:按行输入IP列表加入维护,一行一个IP,多行输入/pp classpad-btm按关键字匹配:主机描述信息的关键字匹配的加入维护,一般用于虚拟机宿主机和关键字匹配/pp classpad-btm匹配子网关键字维护:IP网段匹配的加入维护,一次填写一个子网,多个子网多次设置,写法示例:172.16./pbutton classbtn btn-success btn-block typesubmit nameiplistsb按IP列表维护一般情况用这个就行/b/button/br/brbutton classbtn btn-success btn-block typesubmit namedescsb按关键字匹配/b/buttonbutton classbtn btn-success btn-block typesubmit nameipnetsb匹配子网关键字维护/b/buttonh4 stylecolor: redb{{ login_err }}/b/h4/form/div/div/div /div
/body
/html
跑起来大约是这个界面,用户填写主机IP列表,或匹配的描述,或子网的信息,选好时间,点个按钮就能实现批量的维护任务
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/926061.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!