在使用Zabbix的过程中,我们通常会建立一些需要的Screen图形报表来汇总需要监控的Graph。
 而下面的两个脚本,则是通过从Zabbix数据库中获取所有的Screen图形参数,提供Zabbix的WEB接口将所有图形保存到本地,然后通过脚本以Email形式发送过来,作为每天的自动报表。
 
$ sudo mkdir -p /data/script
$ sudo mkdir -p /data/graph
 
$ sudo vim /data/scripts/save-graph.pl

#!/usr/bin/perl
use File::Path;
use DBI;

my $path = '/data/graph';
if(-e $path) { rmtree($path); }
mkdir($path);

my $stime = `date +%Y%m%d`; chop($stime); $stime .= '1000';
if( length($stime) != 12 ) { print "Error get date"; exit; }

my $period = 86400;    # 24 hours

my $login = 'admin';  # Zabbix Web User
my $pass = 'password'; # Zabbix Web User Password, must be URL Encoded

my $cook = "/tmp/cookie";
my $dsn = 'DBI:mysql:zabbix:localhost'; # Connect MySQL DB "zabbix" on localhost
my $db_user_name = 'zabbix'; # MySQL DB user
my $db_password = 'dbpassword'; # MySQL DB user password

my $dbh = DBI->connect($dsn, $db_user_name, $db_password);
my $sth = $dbh->prepare(qq{select a.name,a.hsize,a.vsize, b.resourceid, b.width, b.height,b.x,b.y from screens a,screens_items as b where a.screenid=b.screenid and a.templateid<=>NULL order by a.name});
$sth->execute();
my %screens;

# Get all graphs by using curl
while (my ($name,$hsize,$vsize, $id,$width,$height,$x,$y) = $sth->fetchrow_array())
{
    if(length($id) > 2){
        #print "$id => $ids\n";
        my $p = "$path/$name.$hsize.$vsize.$y.$x.$id.png";
        my $strcomm  = `curl  -c $cook -b $cook -d "request=&name=$login&password=$pass&autologin=1&enter=Sign+in"  localhost/zabbix/index.php`;
        $strcomm  = `curl  -b $cook -F  "graphid=$id" -F "period=$period" -F "stime=$stime" -F "width=$width" -F "height=$height" localhost/zabbix/chart2.php > $p`;
    }
}

exit ;

$ sudo vim email-pic.py

#! /usr/bin/env python

import os
import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

def _sendmail(smtp_server,port,account,password,str_from,list_to,msg):
    smtp = smtplib.SMTP(smtp_server,port)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login(account, password)
    smtp.sendmail(str_from, list_to,msg)
    smtp.close()

def _get_pictures(image_dir):
    pictures = []
    for f in os.listdir(image_dir):
        pictures.append(f)
    return pictures

def _create_msg(screen_name,screens,image_dir,str_from,list_to):
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = 'Zabbix Screen Report: %s' % screen_name
    msgRoot['From'] = str_from
    msgRoot['To'] = ",".join(list_to)
    msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
    # 'alternative' part, so message agents can decide which they want to display.
    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
    msgAlternative.attach(msgText)
    contents = ""
    contents += "<h1>Screen %s</h1><br>" % screen_name
    _,hsize,vsize,_,_,_,_,= tuple(screens[0].split('.'))
    contents +="<table>"
    screens = sorted(screens)
    y= -1
    for f in screens:
        items = f.split('.')
        _,_,_,image_y,image_x,image_id,_ = tuple(items)
        image_name = "image-%s-%s" % (screen_name, image_id)
        fp = open('%s/%s' % (image_dir,f), 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', "<%s>" % image_name)
        msgRoot.attach(msgImage)
        if y != image_y:
            if y!= -1:
                contents +="</tr>"
            y = image_y
            contents +="<tr>"
        contents +="<td><img src='cid:%s'></td>" % image_name
    contents += "</table>"
    msgText = MIMEText(contents, 'html')
    msgAlternative.attach(msgText)
    msgRoot.attach(msgAlternative)
    return msgRoot

# Create the root message and fill in the from, to, and subject headers
def main(str_from,list_to,image_dir):
    pictures = _get_pictures(image_dir)
    for screen_name in list(set([x.split('.')[0] for x in pictures ])):
        screens = [x for x in pictures if x.startswith(str(screen_name) + '.') ]
        msgRoot = _create_msg(screen_name,screens,image_dir,str_from,list_to)
        _sendmail('smtp.example.com',25,'username','password',str_from,list_to,msgRoot.as_string())

if __name__ == '__main__':
  str_from = 'username@example.com'
  list_to = [
                "jack@example.com", "tom@example.com", "jim@example.com"
            ]
  image_dir = '/data/graph'
  main(str_from,list_to,image_dir) 
接着,将这两个脚本加入到crontab中定时执行。
 
$ sudo crontab -e
 30 23 * * * /data/script/save-graph.pl
 55 23 * * * /data/script/email-pic.py
 
这样,就可以收到对应的Zabbix图形邮件报表了,如下图所示:

-------------------------------分割线-------------------------------

1、按周显示:

my $period = 86400; 这里就是用来定义时间的,以秒为单位。

2、执行python时出错,麻烦帮助看一下。python2.6\2.7版本都试过
 
Traceback (most recent call last):
 File "./email-pic.py", line 80, in
 main(str_from,list_to,image_dir)
 File "./email-pic.py", line 71, in main
 msgRoot = _create_msg(screen_name,screens,image_dir,str_from,list_to)
 File "./email-pic.py", line 41, in _create_msg
 _,hsize,vsize,_,_,_,_,= tuple(screens[0].split('.'))
 IndexError: list index out of range

必须和上面的脚本和步骤配合使用,完成的任务其实就是,创建目录,利用Zabbix接口生成好好图片,然后通过邮件发送。

Zabbix通过邮件发送Screen图形报表实现的更多相关文章

  1. zabbix 使用邮件发送告警信息

    配置系统mail命令,使其可以发送外网邮件 mail 命令配置 修改zabbix_server配置文件,使其可以执行告警脚本 [root@rexen etc]# vim /usr/local/zabb ...

  2. 使用phantomjs实现highcharts等报表通过邮件发送(本文仅提供完整解决方案和实现思路,完全照搬不去整理代码无法马上得到效果)

    前不久项目组需要将测试相关的质量数据通过每日自动生成报表展示,并自动通过将报表作为邮件正文内容知会到干系人邮箱.那么问题来了,报表生成了,但是邮件怎么发送,因为highcharts等报表都是通过JS和 ...

  3. zabbix监控系列(4)之zabbix报警邮件无法发送

    情况介绍 首先确保邮箱规则没有把报警邮件作为垃圾邮件拉黑了. 服务器断电重启后,发现zabbix报警邮件无法发送,断电之前是好好的,但是重启后不行了,于是查看maillog日志,发现这个错误: Hos ...

  4. [原创] zabbix学习之旅六:如何解决zabbix server在内网,而邮件发送服务器在外网的问题

    通过前面的文章,你已经可以快速地搭建一个报警系统,并能正常的收到报警邮件了.不过在很多企业级环境下,邮件发送服务器往往放在外网,而zabbix server放置在内网,在这种情况下,zabbix的报警 ...

  5. 使用phantomjs实现highcharts等报表通过邮件发送

    使用phantomjs实现highcharts等报表通过邮件发送(本文仅提供完整解决方案和实现思路,完全照搬不去整理代码无法马上得到效果)   前不久项目组需要将测试相关的质量数据通过每日自动生成报表 ...

  6. zabbix增加手机短信、邮件监控的注意要点,SSL邮件发送python脚本

    1.短信接口文档: URL http://xxx.com/interfaces/sendMsg.htm Method POST Description 文字短信调用接口 Request Param L ...

  7. zabbix告警邮件、短信发送错误快速排查方法

    zabbix告警邮件.短信发送错误快速排查方法 背景 zabbix告警邮件.短信经常有同事反馈发送错误的情况,这个问题排查的角度很多,那么最快捷的角度是什么呢? 在我看来,最快的角度就是判断这个告警邮 ...

  8. zabbix添加邮件报警机制

    zabbix添加邮件报警机制 作者:尹正杰 还记得之前跟大家聊过的一个如何监控一个目录的话题吗?我们虽然监控出来数据了,也有数据了,但是,只是监控也没有用啊~因为我们不能24小时盯着屏幕然后 出了事情 ...

  9. Java实现多线程邮件发送

    利用java多线程技术配合线程池实现多任务邮件发送. 1.基本邮件发送MailSender package hk.buttonwood.ops.email; import java.io.File; ...

随机推荐

  1. 微信小程序、SSL证书、开启服务器TSL1.0、TSL1.1、TSL1.2服务

    微信小程序.SSL证书.开启服务器TSL1.0.TSL1.1.TSL1.2服务 https://blog.csdn.net/qq_32933615/article/details/70143105

  2. React Native商城项目实战14 - 首页中间下部分

    1.MiddleBottomView.js /** * 首页中间下部分 */ import React, { Component } from 'react'; import { AppRegistr ...

  3. 线性中继器 Linear Repeater

     线性中继器(Linear Repeater,缩写L-REP) 高速信号在传输介质上传递时,信号衰减和噪声会导致有效数据信号越来越弱.L-REP就是用来再生高速信号,通过使用同等化(Equalizat ...

  4. jobs的后台进程程序如何终止?

    好像没有专门的jobs相关的命令来终止后台进程, 只有通过 jobs -l看 后台进程的pid, 然后用kill来终止. 摘录: (( 进程的终止 后台进程的终止: 方法一: 通过jobs命令查看jo ...

  5. C# 内存建表备忘

    #region=====建表===== DataSet dataSet; // 创建表 DataTable table = new DataTable("testTable"); ...

  6. 如何让字典保持有序---Python数据结构与算法相关问题与解决技巧

    实际案例: 某编程竞赛系统,对参赛选手编程解体进行计时,选手完成题目后,吧该选手解体用时记录到字典中,以便赛后按选手名查询成绩 {'Lilei':(2,43),'HanMei':(5,52),'Jim ...

  7. DFS序1

    给一棵有根树,这棵树由编号为1..N的N个结点组成.根结点的编号为R.每个结点都有一个权值,结点i的权值为vi .接下来有M组操作,操作分为两类:1 a x,表示将结点a的权值增加x:2 a,表示求结 ...

  8. Common Linux Commands 日常工作常用Linux命令

      How to know CPU info      cat /proc/cpuinfo      arch   How to know memory info: cat /proc/meminfo ...

  9. 简述移动端与PC端的区别

    1.移动端与PC端的区别 PC考虑的是浏览器的兼容性,而移动端开发考虑的更多的是手机兼容性,因为目前不管是android手机还是ios手机,一般浏览器使用的都是webkit内核,所以说做移动端开发,更 ...

  10. 实验报告3&学习总结

    1.已知字符串:"this is a test of java".按要求执行以下操作: 统计该字符串中字母s出现的次数. 统计该字符串中子串"is"出现的次数. ...