基于zabbix 自动抓取每天监控数据!/usr/local/python/bin/python3.5

2018-04-11 15:32:29 浏览数 (1)

!/usr/local/python/bin/python3.5

import sys, os, shutil import os.path import datetime import http.cookiejar import urllib.request import urllib.error import urllib.parse import smtplib from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage class ZabbixGraph(object): def init(self, url, name, password): self.url = url self.name = name self.password = password cookiejar = http.cookiejar.CookieJar() urlOpener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar)) values = {"name":self.name,'password':self.password,'autologin':1,"enter":'Sign in'} data = urllib.parse.urlencode(values).encode(encoding='UTF8') request = urllib.request.Request(url, data) try: urlOpener.open(request, timeout=10) self.urlOpener = urlOpener except urllib.error.HTTPError as e: print(e)

代码语言:javascript复制
def getgraph(self, url, values, image_dir):
    key = values.keys()
    if 'graphid' not in key:
        # print('请确认是否输入graphid')
        sys.exit(1)
    if 'period' not in key:
        values['period'] = 86400
    if 'stime' not in key:
        values['stime'] = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    if 'width' not in key:
        values["width"] = 800
    if 'height' not in key:
        values["height"] = 200

    data = urllib.parse.urlencode(values).encode(encoding='UTF8')
    request = urllib.request.Request(url, data)
    url = self.urlOpener.open(request)
    image = url.read()
    imagename = "%s/%s.png" % (image_dir, values["graphid"])
    f = open(imagename, 'wb')
    f.write(image)

def yesterday(): now_time = datetime.datetime.now() yes_time = now_time datetime.timedelta(days=-1) yes_time_nyr = yes_time.strftime('%Y%m%d') yes_time_nyr1 = yes_time.strftime('%Y-%m-%d') return yes_time_nyr, yes_time_nyr1

def email(): sender = '发送者' receiver = ['接收者1', '接收者2', '接收者3'] subject = '每日重点监控对象' smtpserver = 'smtp.126.com' username = '发送者邮箱' password = 'smtp密码' msgRoot = MIMEMultipart('related') text = yesterday1 ' 重点监控数据报告已生成。n请访问:http://xxx/' yesterday '/index.html' msg = MIMEText(text, 'plain', 'utf-8') # 中文需参数‘utf-8',单字节字符不需要 msg['Subject'] = Header(subject, 'utf-8') msg['From'] = 'Robot<xxx>' msg['To'] = '接收者别名' smtp = smtplib.SMTP() smtp.connect('smtp.126.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()

def html(): all_the_text = '''<html> <head> <meta charset="utf8"> <script type="text/javascript" href="jquery-3.1.0.min.js"></script> <style type="text/css"> body{ text-align: center; } .middle{ text-align: center; } .hide{ display: none; } .show{ display: block; } </style> <title>每日数据报告</title> </head> <body> <h1 class="middle">''' yesterday1 '''监控数据报告</h1> <div> <select id="sel"> <option value="op_01">xxx</option> <option value="op_02">xxx</option> <option value="op_03">xxx</option> <option value="op_04">xxx</option> </select> </div> <div > <div class="con show" id="op_01">

</div> <div class="con hide" id="op_02">

</div> <div class="con hide" id="op_03">

</div> <div class="con hide" id="op_04">

</div> </div>

代码语言:javascript复制
    <script type="text/javascript" src="jquery-3.1.0.min.js"></script>
    <script type="text/javascript">
        $('#sel').change(function(){
            var cid = $(this).val();
            $('#' cid).show();
            $('#' cid).siblings().hide();
        });
    </script>
</body>
</html>'''

with open(image_dir   '/index.html', 'w') as f:
    f.write(all_the_text)

src = '/usr/monitor/day/jquery-3.1.0.min.js'
dst = '/usr/monitor/day/'   yesterday   '/jquery-3.1.0.min.js'
shutil.copyfile(src, dst)

if name=='main': yesterday, yesterday1 = yesterday() gr_url="http://xxx/zabbix/chart2.php" indexURL="http://xxx/zabbix/index.php" username = 'xxx' password = 'xxx' os.mkdir('/usr/monitor/day/%s' % yesterday) image_dir='/usr/monitor/day/' yesterday

代码语言:javascript复制
values1={"graphid":"1148","period":86400,"stime":yesterday '000000',"width":800,"height":200}

values4={"graphid":"1145","period":86400,"stime":yesterday '000000',"width":800,"height":200}

values5 = {"graphid": "1079", "period": 86400, "stime": yesterday   '000000', "width": 800, "height": 200}

values5_1 = {"graphid": "792", "period": 86400, "stime": yesterday   '000000', "width": 800, "height": 200}
b=ZabbixGraph(indexURL,username,password)
for i in (values1, values4, values5, values5_1):
    b.getgraph(gr_url, i, image_dir)

html()
email()

0 人点赞