码迷,mamicode.com
首页 > 编程语言 > 详细

zabbix,自动化日报,python2

时间:2020-10-08 18:41:56      阅读:20      评论:0      收藏:0      [点我收藏+]

标签:join   方式   ESS   error   zab   user   out   cpp   登录   

-- coding:utf-8 --

import requests
from pptx.util import Inches
import os
from PIL import Image
import json
import urllib2
import datetime
import time

current_date = datetime.datetime.now()
print u"当前采集的是%s的数据,请确认日期" % (current_date - datetime.timedelta(days=1)).strftime("%Y-%m-%d")

time.sleep(10)
report_date = current_date - datetime.timedelta(days=1)

print u"正在采集报表文件,请稍等"
print u‘正在生成%s年%s月%s日天翼阅读平台运维日报‘ % (report_date.year,report_date.month,report_date.day)

#图片元素要和PPT的slide对应
picture_data = {
‘tingshu‘:{
‘data‘:{‘slide6‘:[
{‘name‘:‘61bond0‘,‘id‘:‘2662‘,‘position‘:6},
{‘name‘:‘61eth3‘,‘id‘:‘2661‘,‘position‘:6},
{‘name‘:‘64bond0‘,‘id‘:‘2669‘,‘position‘:6},
{‘name‘:‘641eth3‘,‘id‘:‘2668‘,‘position‘:6},
],
‘slide7‘:[
{‘name‘:‘615bond0‘,‘id‘:‘2892‘,‘position‘:7},
{‘name‘:‘615bond1‘,‘id‘:‘2893‘,‘position‘:7},
{‘name‘:‘617bond0‘,‘id‘:‘2988‘,‘position‘:7},
{‘name‘:‘617bond1‘,‘id‘:‘2989‘,‘position‘:7}
],
‘slide8‘: [
{‘name‘:‘620bond0‘,‘id‘:‘3083‘,‘position‘:8},
{‘name‘:‘620bond1‘,‘id‘:‘3084‘,‘position‘:8},
{‘name‘:‘621bond0‘,‘id‘:‘2961‘,‘position‘:8},
{‘name‘:‘621bond1‘,‘id‘:‘2962‘,‘position‘:8},
],
‘slide9‘:[
{‘name‘:‘623bond0‘,‘id‘:‘2970‘,‘position‘:9},
{‘name‘:‘623bond1‘,‘id‘:‘2971‘,‘position‘:9},
{‘name‘:‘625bond0‘,‘id‘:‘2979‘,‘position‘:9},
{‘name‘:‘625bond1‘,‘id‘:‘2980‘,‘position‘:9},
]
},
‘server_url‘:‘http://172.23.1.67/zabbix‘,
‘site‘:‘0‘,
‘style‘:‘1‘,
‘width‘:4.6,
‘height‘:2.3,
‘is_resize‘:False
},
‘jifei‘:{
‘data‘:{
‘slide10‘:[
{‘name‘:‘xjf‘,‘id‘:‘12839‘,‘position‘:10},
{‘name‘:‘dd‘,‘id‘:‘5545‘,‘position‘:10},
{‘name‘:‘emp‘,‘id‘:‘12462‘,‘position‘:10},
{‘name‘:‘yijifen‘,‘id‘:‘15076‘,‘position‘:10},
],
‘slide11‘:[
{‘name‘:‘hlxk_delay_new‘,‘id‘:‘4922‘,‘position‘:11},
{‘name‘:‘hlxk_delay_old‘,‘id‘:‘4233‘,‘position‘:11},
{‘name‘:‘hlxk_response_old‘,‘id‘:‘4540‘,‘position‘:11},
{‘name‘:‘hlxk_response_new‘,‘id‘:‘4703‘,‘position‘:11},
],
‘slide12‘:[
{‘name‘:‘emp_tcpping‘,‘id‘:‘12462‘,‘position‘:12},
{‘name‘:‘tykj_tcpping‘,‘id‘:‘13650‘,‘position‘:12},
{‘name‘:‘woread_tcpping‘,‘id‘:‘13651‘,‘position‘:12},
{‘name‘:‘duandai_tcpping‘,‘id‘:‘13652‘,‘position‘:12},
],
‘slide13‘:[
{‘name‘:‘heread_tcpping‘,‘id‘:‘13653‘,‘position‘:13},
{‘name‘:‘xiaowo_tcpping‘,‘id‘:‘13654‘,‘position‘:13},
{‘name‘:‘wechat_tcpping‘,‘id‘:‘13655‘,‘position‘:13},
{‘name‘:‘alipay_bankcard_tcpping‘,‘id‘:‘13656‘,‘position‘:13},
],
‘slide14‘:[
{‘name‘:‘llkj_bankcard_tcpping‘,‘id‘:‘13662‘,‘position‘:14},
{‘name‘:‘shenzhou_pay_tcpping‘,‘id‘:‘13657‘,‘position‘:14},
{‘name‘:‘alipay_tcpping‘,‘id‘:‘13658‘,‘position‘:14},
{‘name‘:‘yi_pay_tcpping‘,‘id‘:‘13659‘,‘position‘:14},

{‘name‘:‘yijifen‘,‘id‘:‘15076‘,‘position‘:14},

            ],

            ‘slide16‘:[
                {‘name‘:‘tykj_log_error_report_1‘,‘id‘:‘14163‘,‘position‘:16},
                {‘name‘:‘tykj_log_error_report_2‘,‘id‘:‘14164‘,‘position‘:16},
                {‘name‘:‘woread_log_error_report_1‘,‘id‘:‘14165‘,‘position‘:16},
                {‘name‘:‘woread_log_error_report_2‘,‘id‘:‘14166‘,‘position‘:16},
            ]
      },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘1‘,
      ‘style‘:‘1‘,
      ‘width‘:4.6,
      ‘height‘:2.3,
      ‘is_resize‘:False         
    },
  ‘xiaoji‘:{
      ‘data‘:{
        ‘slide15‘:[
            {‘name‘:‘youmi_tcpping‘,‘id‘:‘13660‘,‘position‘:15},
            {‘name‘:‘duiba_tcpping‘,‘id‘:‘13661‘,‘position‘:15},
            ],
              ‘slide17‘:[
                        {‘name‘:‘xiaoji1‘,‘id‘:‘4720‘,‘position‘:17},
                        {‘name‘:‘xiaoji2‘,‘id‘:‘4822‘,‘position‘:17},
                      ]
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘2‘,
      ‘style‘:‘2‘,         
      ‘width‘:6.2,
      ‘height‘:1.8,
      ‘is_resize‘:False
    },
    ‘liuliang‘:{
            ‘data‘:{
                    ‘slide2‘:[
                        {‘name‘:‘xf174‘,‘id‘:‘12992‘,‘position‘:2},
                        {‘name‘:‘xf175‘,‘id‘:‘12973‘,‘position‘:2},
                        {‘name‘:‘mh‘,‘id‘:‘3382‘,‘position‘:2},#修正门户和支付接口ID错误
                        {‘name‘:‘zfjk‘,‘id‘:‘5503‘,‘position‘:2},
                    ]
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘1‘,
      ‘style‘:‘1‘,
      ‘width‘:4.6,
      ‘height‘:2.3,
      ‘is_resize‘:False
    },
    ‘wangluo‘:{
            ‘data‘:{
                    ‘slide19‘:[
                        {‘name‘:‘jhj1‘,‘id‘:‘5756‘,‘position‘:19},
                        {‘name‘:‘jhj2‘,‘id‘:‘5747‘,‘position‘:19},
                        {‘name‘:‘asa_connections‘,‘id‘:‘3456‘,‘position‘:19},
                        {‘name‘:‘asa_cpu‘,‘id‘:‘3461‘,‘position‘:19},
                    ]
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘2‘,
      ‘style‘:‘1‘,
      ‘width‘:4.6,
      ‘height‘:2.3,
      ‘is_resize‘:False               
        },

    ‘scf_load‘:{
            ‘data‘:{
                    ‘slide21‘:[
                        {‘name‘:‘cic_load‘,‘id‘:‘13143‘,‘position‘:21},
                        {‘name‘:‘oic_load‘,‘id‘:‘13134‘,‘position‘:21},
                        {‘name‘:‘pic_load‘,‘id‘:‘13138‘,‘position‘:21},
                        {‘name‘:‘uic_load‘,‘id‘:‘13141‘,‘position‘:21},                         
                    ]
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘2‘,
      ‘style‘:‘1‘,
      ‘width‘:4.6,
      ‘height‘:2.3,
      ‘is_resize‘:True
        },       
    ‘scf_traffic‘:{
            ‘data‘:{
                    ‘slide16‘:[
                        {‘name‘:‘cic_in‘,‘id‘:‘13142‘,‘position‘:22},
                        {‘name‘:‘cic_out‘,‘id‘:‘13144‘,‘position‘:22},
                        {‘name‘:‘oic_in‘,‘id‘:‘13133‘,‘position‘:22},
                        {‘name‘:‘oic_out‘,‘id‘:‘13135‘,‘position‘:22},
                    ],
                    ‘slide17‘:[
                        {‘name‘:‘pic_in‘,‘id‘:‘13136‘,‘position‘:23},
                        {‘name‘:‘pic_out‘,‘id‘:‘13137‘,‘position‘:23},
                        {‘name‘:‘uic_in‘,‘id‘:‘13139‘,‘position‘:23},
                        {‘name‘:‘uic_out‘,‘id‘:‘13140‘,‘position‘:23},
                    ],
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘2‘,
      ‘style‘:‘1‘,
      ‘width‘:4.6,
      ‘height‘:2.3,
      ‘is_resize‘:True               
        },

    ‘mhxf_load‘:{
            ‘data‘:{
                    ‘slide24‘:[
                        {‘name‘:‘mh_all_load‘,‘id‘:‘13145‘,‘position‘:24},
                        {‘name‘:‘xf_all_load‘,‘id‘:‘13151‘,‘position‘:24},                         
                    ]
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘2‘,
      ‘style‘:‘2‘,
      ‘width‘:6.2,
      ‘height‘:1.8,
      ‘is_resize‘:True               
        },
    ‘nginx‘:{
            ‘data‘:{
                    ‘slide25‘:[
                        {‘name‘:‘mh_nginx‘,‘id‘:‘13255‘,‘position‘:25},
                        {‘name‘:‘xf_nginx‘,‘id‘:‘13256‘,‘position‘:25},                         
                    ]
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘2‘,
      ‘style‘:‘2‘,
      ‘width‘:6.2,
      ‘height‘:1.8,
      ‘is_resize‘:True
        },
    ‘mhxf_traffic‘:{
            ‘data‘:{
                    ‘slide26‘:[
                        {‘name‘:‘mh_all_in‘,‘id‘:‘13146‘,‘position‘:26},
                        {‘name‘:‘mh_all_out‘,‘id‘:‘13148‘,‘position‘:26},
                        {‘name‘:‘xf_all_in‘,‘id‘:‘13149‘,‘position‘:26},
                        {‘name‘:‘xf_all_out‘,‘id‘:‘13150‘,‘position‘:26},                             
                    ]
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘2‘,
      ‘style‘:‘1‘,
      ‘width‘:4.6,
      ‘height‘:2.3,
      ‘is_resize‘:True
        },

    ‘original_tcpping‘:{
            ‘data‘:{
                    ‘slide27‘:[
                        {‘name‘:‘10254‘,‘id‘:‘5761‘,‘position‘:27},
                        {‘name‘:‘30254‘,‘id‘:‘14171‘,‘position‘:27},
                        {‘name‘:‘247179‘,‘id‘:‘14173‘,‘position‘:27},
                        {‘name‘:‘76242‘,‘id‘:‘12568‘,‘position‘:27},                             
                    ]
        },
      ‘server_url‘:‘http://192.168.11.4‘,
      ‘site‘:‘2‘,
      ‘style‘:‘1‘,
      ‘width‘:4.6,
      ‘height‘:2.3,
      ‘is_resize‘:True
        },

}

#利用zabbix API 进行登录验证换取cookie以便于采取需要权限的图片
zabbix_url="http://192.168.11.4/api_jsonrpc.php"
zabbix_header = {"Content-Type":"application/json"}

auth_data = json.dumps({
"jsonrpc":"2.0",
"method":"user.login",
"params":
{
"user":"admin",
"password":"ytxt_zabbix"
},
"id":0
})

request = urllib2.Request(zabbix_url,auth_data)
for key in zabbix_header:
request.add_header(key,zabbix_header[key])

result = urllib2.urlopen(request)
response=json.loads(result.read())

#简单的图片存储方式,当前目录下需要手工新建image目录
def image_spider(url=‘‘,graphid=‘‘,cookie=‘‘,gra_name=‘‘):
PWD = "image"
if not os.path.exists(os.path.join(PWD)):os.mkdir(PWD)
target_url ="%s/chart2.php?graphid=%s&period=86400&stime=%s000000" % (url,graphid,report_date.strftime(‘%Y%m%d‘))
print target_url
c = requests.get(target_url,stream=True,cookies=cookie)
name = gra_name+".png"
with open(PWD+‘/‘+name, ‘wb‘) as fd:
for chunk in c.iter_content():
fd.write(chunk)

for value in picture_data:
for v in picture_data[value][‘data‘]:
for index,val in enumerate(picture_data[value][‘data‘][v]):

        style = picture_data[value][‘style‘]

        if style=="1":
            left = index%2==0 and Inches(0.25) or Inches(5.23)
            top = index<=1 and Inches(1.90) or Inches(5)
        else:
            left =  Inches(2)
            top = index%2==0 and Inches(1.90) or Inches(5.2)

        height = Inches(picture_data[value][‘height‘])
        width=Inches(picture_data[value][‘width‘])

        if picture_data[value][‘site‘] =="0":
            fetch_cookie = ‘‘
        else:
            fetch_cookie = {‘zbx_sessionid‘:response[‘result‘]}

        image_spider(picture_data[value][‘server_url‘],val[‘id‘],fetch_cookie, val[‘name‘])
        img_path="image/%s.png" % val[‘name‘]
        if picture_data[value][‘is_resize‘]:
            box = (0,0,1016,310)
            im = Image.open(img_path)
            region=im.crop(box)
            region.save(img_path)
        print u"第%d张幻灯片中ID为%s的图片插入成功" % (val[‘position‘]+1,val[‘id‘])

zabbix,自动化日报,python2

标签:join   方式   ESS   error   zab   user   out   cpp   登录   

原文地址:https://blog.51cto.com/13420391/2540297

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!