zabbix php 转python,如何通过python获取zabbix中的graph报表

# -*- coding: utf8 -*-

'''

@author: zhiming www.503error.com

'''

import urllib2

import urllib

import cookielib

import hashlib

import re

import time

from rhsm.certificate import Content

import smtplib

from os.path import basename

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.utils import COMMASPACE, formatdate

import os

class ZABBIX:

def __init__(self):

self.usercode = ""

self.userid = ""

self.cj = cookielib.LWPCookieJar();

self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj));

urllib2.install_opener(self.opener);

def login(self, name, pwd,urls):

loginPostData = {

'name' : name,

'password' : pwd ,

'autologin':1 ,

'enter': 'Sign in',

'request':''

};

loginRequest = urllib2.Request('https://localhost/zabbix/index.php',urllib.urlencode(loginPostData));

loginRequest.add_header('Accept','*/*');

loginRequest.add_header('Accept-Language','zh-CN,zh;q=0.8');

loginRequest.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36');

loginRequest.add_header('Content-Type','application/x-www-form-urlencoded');

sendPost = urllib2.urlopen(loginRequest).read();

print 'start donwload'

for key,value in downloadurl.items():

print 'key',key

print 'value',value

currenttime = time.strftime("%s")

currentday = time.strftime("%y%m%d")

if os.path.exists(key+currentday+".png"):

print 'already donwload ,ignore'

else:

print 'start download :',key

try:

f = urllib2.urlopen(value,timeout=60)

data = f.read()

#print 'the data is %s'% data

with open(key+currentday+'.png', "wb") as code:

code.write(data)

print 'done the download'

except Exception,e:

print e

def sendtocus(self,send_from, send_to, subject, text, files=None,

server="127.0.0.1"):

msg = MIMEMultipart()

msg['From'] = send_from

msg['To'] = send_to

msg['Date'] = formatdate(localtime=True)

msg['Subject'] = subject

msg.attach(MIMEText(text))

for f in files or []:

with open(f, "rb") as fil:

part = MIMEApplication(

fil.read(),

Name=basename(f)

)

part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)

msg.attach(part)

smtp = smtplib.SMTP(server)

smtp.sendmail(send_from, send_to, msg.as_string())

smtp.close()

if __name__ == "__main__":

test = ZABBIX()

downloadurl={"master-cpu":"https://localhost/zabbix/chart.php?sid=c6eaf2cf103&period=604800&stime=20180811034616&itemids%5B0%5D=2187887&itemids%5B1%5D=2188075&itemids%5B2%5D=2188263&type=1&batch=1&updateProfile=0&profileIdx=&profileIdx2=&width=1136&screenid=&curtime=1472111226924",

"master-mem":"https://localhost/zabbix/chart.php?sid=c6eaff103&period=604800&stime=20180811034616&itemids%5B0%5D=2187887&itemids%5B1%5D=2188075&itemids%5B2%5D=2188263&type=1&batch=1&updateProfile=0&profileIdx=&profileIdx2=&width=1136&screenid=&curtime=1472111226924"

}

test.login('maple_m', '****your password***',downloadurl)

files = []

todaydate = time.strftime("%y%m%d")

for key,value in downloadurl.items():

files.append(key+todaydate+".png")

print 'start send email'

content_of_email = """

Hi

this is the report of this week

thanks

OpenShift Online Operation

"""

test.sendtocus("maple_m@hotmail.com", "maple_m@hotmail.com", "Weekly report of "+todaydate, content_of_email, files)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
zatree 是来自国内58公司开发的监控软件zabbix的一个插件,主要功能是提供host group的树形展示和在item里指定关键字查询及数据排序。 安装方法: 1:下载文件 git clone https://github.com/spide4k/zatree.git zatree 2:复制相关文件 假如zabbix web目录位置在/var/www/zabbix,定义zabbix目录 ZABBIX_PATH=/var/www/zabbix 复制相关文件和目录 cp -rf zatree $ZABBIX_PATH/ cd $ZABBIX_PATH/zatree/addfile cp class.cchart_zabbix.php class.cgraphdraw_zabbix.php class.cimagetexttable_zabbix.php $ZABBIX_PATH/include/classes/ cp zabbix.php zabbix_chart.php $ZABBIX_PATH/ cp CItemValue.php $ZABBIX_PATH/api/classes/ 3:支持web interface,修改配置文件 vi $ZABBIX_PATH/zatree/zabbix_config.php 'user'=>'xxx', //你的用户名 'passowrd'=>'xxx', //你的密码 4:导航增加Zatree入口,修改menu.inc.php,main.js vi $ZABBIX_PATH/include/menu.inc.php 添加285行到294行内容 'zatree'=>array( 'label' => _('zatree'), 'user_type' => USER_TYPE_ZABBIX_USER, 'default_page_id' => 0, 'force_disable_all_nodes' => true, 'pages' =>array( array('url' => 'zabbix.php','label' => _('Zatree'),) ) ), 'login' => array( 'label' => _('Login'), 'user_type' => 0, 'default_page_id' => 0, vi $ZABBIX_PATH/js/main.js 替换106行 menus: {'empty': 0, 'view': 0, 'cm': 0, 'reports': 0, 'config': 0, 'admin': 0, 'zatree':0}, 6:增加封装的api类 vi $ZABBIX_PATH/include/classes/api/API.php 在74行下添加75行'itemvalue'=>'CItemValue', 'usermedia' => 'CUserMedia', 'itemvalue'=>'CItemValue', 'webcheck' => 'CWebCheck' ); 7:登陆zabbix,在导航里可以看到一个Zatree的菜单,使用方法是傻瓜的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值