orabbix结合python发送图形报表(二) (r6笔记第38天)

时间:2022-05-04
本文章向大家介绍orabbix结合python发送图形报表(二) (r6笔记第38天),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在之前的博文中分享过通过结合python来发送图形报表邮件的例子。 http://blog.itpub.net/23718752/viewspace-1776784/ 当然我们还是需要实现,意味着那些碰到的硬骨头都需要啃下来,大体的思路如下,每个步骤都有一些难点。 1.知道screenid和graphid和mysql表的关联关系 2.利用screenid和graphid得到对应的报表图片 3.把图形存储在临时目录下 4.把图形以附件的形式发送。 首先就是表的关联。这个部分还真没有什么捷径,最好的方法就是自己去根据里面的数据去找规律,至少从我的尝试,没有找到对应的datamodel之类的文档,不过这个开源的一个优点就是表定义还是很规范的,能够根据字面意思就基本能够看出来对应的数据含义。 我们可以根据图片的链接地址来倒推,链接地址一般为http://zabbix.xxx.com/chart2.php?graphid=524&screenid=22&width=400&height=156&period=86400 的形式,可以根据url看出screenid的部分,这个screenid其实就是做数据推理的关键。在MySQL的库中这个表的定义如下: [zabbix] [11:43:34]> desc screens +------------+---------------------+------+-----+ | Field | Type | Null | Key | +------------+---------------------+------+-----+ | screenid | bigint(20) unsigned | NO | PRI | | name | varchar(255) | NO | | | hsize | int(11) | NO | | | vsize | int(11) | NO | | | templateid | bigint(20) unsigned | YES | MUL | +------------+---------------------+------+-----+ 根据screenid就能够得到screen的基本信息 [zabbix] [09:56:49]> select screenid from screens where name='Oracle Graphs'; +----------+ | screenid | +----------+ |22 | +----------+ 然后根据screen的信息和对应的screen_item结合起来,screen_item和监控项是有关联的,比如我们的图形中某个监控图中需要显示active session数,inactive session数,那么这两个就是screen_item,他们有会对应相应的监控项,比如screenid=22,screenitem=86,就能找到对应的一个resourceid [zabbix] [10:51:22]> select resourceid,screenid,screenitemid from screens_items where resourceid=627; +------------+----------+--------------+ | resourceid | screenid | screenitemid | +------------+----------+--------------+ | 627 | 22 | 86 | +------------+----------+--------------+ 这个时候要多提一句,一般的资料中都会说这个resourceid就是graph的id,其实这个是相对的,在我的例子里面,我配置的监控项都是基于一个Oracle监控的模板,然后对于Oracle相关的服务器都添加这个模板进行统一管理,所以查看graphs的时候,其实graphs的templateid(非空)和resourceid是对应的。 [zabbix] [10:19:23]> select *from graphs where name like '%arch%'; +---------+-------------+-------+--------+----------+----------+------------+ | graphid | name | width | height | yaxismin | yaxismax | templateid | +---------+-------------+-------+--------+----------+----------+------------+ | 627 | Archivelog | 900 | 200 | 0.0000 | 100.0000 | NULL | | 665 | Archivelog | 900 | 200 | 0.0000 | 100.0000 | 627 | | 678 | Archivelog | 900 | 200 | 0.0000 | 100.0000 | 627 | | 770 | Archivelog | 900 | 200 | 0.0000 | 100.0000 | 627 | 所以我们知道某一个screenid,要得到对应的graphid,就可以使用下面的方式来查询。 select *from graphs where templateid in (select resourceid from screens_items where screenid=22) and templateid is not null; 这会得到一系列graphid的列表,我们可以根据templateid进行进一步的过滤。 这个过程完成的时候再回过头来就会发现,第1,2步我们都解决了。 第3步就是把对应的图片下载下来生成在对应的目录中。 当然这个方法也很多,python只是一种实现的思路。鉴于mysql+python的结合确实很轻巧和强大。当然python结合mysql还是需要对应的安装包的。 如果有yum就可以直接下载一个。 yum install MySQL-python -y 。。。。。 Installed: MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6 下载图片的部分,需要进行大量的url解析,也是术业有专攻。 可以使用下面的Python代码块来实现。 这个部分会在mysql进行表的关联,通过screen得到对应的screenid,然后进一步匹配,得到需要的graphid,我们在这个地方假定只需要做templateid=1047的graph的下载。 它会把属于对应的模板中所有相关的数据库实例的那个监控项对应的图片都给下载下来。

def get_graph(zabbix_host,username,password,screen,width,height,period,save_graph_path):
    screenid_list = []
    global html
    html = ''
    for i in mysql_query("select screenid from screens where name='%s'"%(screen)):
                for screenid in i:
                    graphid_list = []
                    #for c in mysql_query("select resourceid from screens_items where screenid='%s'"%(int(screenid))):
                    for c in mysql_query("select graphid from graphs where templateid=1047 "):
                                            for d in c:
                            graphid_list.append(int(d))
                    for graphid in graphid_list:
                        login_opt = urllib.urlencode({
                        "name": username,
                        "password": password,
                        "autologin": 1,
                        "enter": "Sign in"})
                        get_graph_opt = urllib.urlencode({
                        "graphid": graphid,
                        "screenid": screenid,
                        "width": width,
                        "height": height,
                        "period": period})
                        cj = cookielib.CookieJar()
                        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
                        login_url = r"http://%s/index.php"%zabbix_host
                        save_graph_url = r"http://%s/chart2.php"%zabbix_host
                        opener.open(login_url,login_opt).read()
                        data = opener.open(save_graph_url,get_graph_opt).read()
                        filename = "%s/%s.%s.png"%(save_graph_path,screenid,graphid)
                        html += '<img width="600" height="250" src="http://%s/%s/%s/%s.%s.png">'%(zabbix_host,save_graph_path.split("/")[len(save_graph_path.split("/"))-2],save_graph_path.split("/")[len(save_graph_path.split("/"))-1],screenid,graphid)
                        f = open(filename,"wb")
                        f.write(data)
                        f.close()

至于发送邮件,还是继续套用python的工具包来做。可以采用下面的方式来发送html格式的邮件。

def send_mail2(user,subject,content):
        me = mail_head+"<"+mail_user+"@"+mail_postfix+">"
        print me
       # msg = MIMEText(content,'plain','utf-8')
        msg = MIMEText(content,_subtype="html",_charset="utf8")
        #msg['Subject'] = Header(subject,'utf-8')
        msg['From'] = me
        msg['to'] = user
        global sendstatus
        global senderr
        try:
                smtp = smtplib.SMTP()
                smtp.connect(mail_host)
                #smtp.login(mail_user,mail_pass)
                smtp.sendmail(me,user,msg.as_string())
                smtp.close()
                print 'send ok'
                sendstatus = True
        except Exception,e:
                senderr = str(e)
                print senderr
                sendstatus = False
        logging.debug(user + ' ' + subject + ' ' + content)

这两个部分都结合起来,得到图片,然后作为附件发送邮件,整个监控发送图形报表的工作就基本完成了。 可能有些地方还是没有说的很清楚,我们可以继续讨论。我也在不断完善这个部分。