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

!/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)

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>

    <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

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 条评论
登录 后参与评论

相关文章

来自专栏图像识别与深度学习

Bluetooth4_3运行流程(连接发射器SN00000009)

31460
来自专栏Kubernetes

Kubernetes如何通过Devi

Device Plugins Device Pulgins在Kubernetes 1.10中是beta特性,开始于Kubernetes 1.8,用来给第三方设备...

54880
来自专栏恰童鞋骚年

.NET Core微服务之基于Ocelot+IdentityServer实现统一验证与授权

  这里,假设我们有两个客户端(一个Web网站,一个移动App),他们要使用系统,需要通过API网关(这里API网关始终作为客户端的统一入口)先向Identit...

39440
来自专栏草根专栏

从头编写 asp.net core 2.0 web api 基础框架 (3)

Github源码地址:https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-...

50770
来自专栏Kubernetes

原 深入分析Kubernetes Sche

21440
来自专栏比原链

Derek解读Bytom源码-P2P网络 地址簿

Gitee地址:https://gitee.com/BytomBlockchain/bytom

9310
来自专栏张善友的专栏

HTTP Basic Authentication验证WCF Data Service

WCF Data Service是OData协议,也是RESTFul Service的一种,上篇文章已经介绍了HTTP Basic Authentication...

21680
来自专栏伪君子的梦呓

网络安全实验室(基础关) writeup~上

因为参加了一个比赛(网鼎杯),所以要快速入门一下信息安全,没想到赛制是线上采用 CTF 的赛制,所以就找了点题目做了一下。

49620
来自专栏全栈架构

Elasticsearch Java API 搜索之Scrolls(四)

首先需要阅读 [scroll documentation](https://www.elastic.co/guide/en/elasticsearch/refe...

58040
来自专栏Kubernetes

深入分析Kubernetes Scheduler的优先级队列

从1.9版本开始,Kubernetes实现了基于Pod优先级的调度队列,一方面提供高优先级的Pod优先被调度的能力,另一方面减轻抢占式调度时潜在的High Pr...

80570

扫码关注云+社区

领取腾讯云代金券