之前搜了一下,几乎没有文章去说zabbix批量如何到处月报表中图片的。

通过zabbix来判断获取报表的url



zabbix通过url来生成图片

http://192.168.100.254/zabbix/chart_bar.php?config=3&title=%E6%8A%A5%E8%A1%A8+3&xlabel=&ylabel=&scaletype=3&avgperiod=2&showlegend=1&report_timesince=1414771200&report_timetill=1417363200&items[0][itemid]=24690&hostids[10123]=10123&hostids[10124]=10124&hostids[10114]=10114&hostids[10119]=10119&hostids[10120]=10120&hostids[10122]=10122&hostids[10111]=10111&hostids[10112]=10112&hostids[10110]=10110&hostids[10126]=10126&hostids[10127]=10127&hostids[10121]=10121&palette=0&palettetype=0

以上链接为根据需求生成报表的时候,可以看到的连接,其中hostidsxxxx】便是主机,items0】【itemid=xxxxx为监控的项目。

report_timesince=1414771200&report_timetill=1417363200为开始时间和结束时间,显示为unix时间戳(以197011000分为0开始)的形式

 构建curl脚本

windows下新建一个yuebao.bat文件,右键编辑,后开始写入脚本文件

关键代码解释

for /f %%i in (3.txt)do set %%i

set a

set b

 

set"c=hostids[10123]=10123&hostids[10124]=10124&hostids[10114]=10114&hostids[10119]=10119&hostids[10120]=10120&hostids[10122]=10122&hostids[10111]=10111&hostids[10112]=10112&hostids[10110]=10110&hostids[10126]=10126&hostids[10127]=10127&hostids[10121]=10121"

 

curl -bcookie.txt -o cpu_idle.png -s -d"title=%E6%8A%A5%E8%A1%A8+3&xlabel=&ylabel=&scaletype=3&avgperiod=2&showlegend=1&report_timesince=%a%&report_timetill=%b%&items[0][itemid]=24672&%c%&palette=0&palettetype=0"   http://192.168.100.254/zabbix/chart_bar.php?config=3

for /f %%i in (3.txt)do set %%i  这句为从外部3.txt文件中读取,读取内容为ab的数字

set a set b 设定两个变量来记录时间,a为起始时间,b为终止时间,windows脚本中引用变量的方式是%a%.

设定变量c为主机群,如果需要新加主机,则在c中将新的主机的hostid写入。

Curl –b cookie.txt 为引用cookie.txt中的内容,用来模拟登陆,不登陆是获取不到数值。

获取cookie的方法为

Curl –c cookie –d “request==&name=admin&password=zabbix&autologin=1&enter=Sign+in”http://192.168.100.254/zabbix/index.php

-o xxx.png为存储图片,-d为 以post方式提出参数,最后访问的网址为  http://192.168.100.254/zabbix/chart_bar.php?config=3

根据日期获取unix时间戳

需要通过c语言编写程序,笔者使用的工具为dev-c++


以下为程序源码

 

 

#include <iostream>

#include <stdlib.h> 

#include <time.h>

#include <string.h>

 

using namespace std;

int main()

{      

       FILE *fp;

       fp=fopen("D:\\3.txt","w");

       struct tm tm;

       time_t t_;

       char s[100];

       int x,y;

       cout <<"input firstyear"<<endl;

       cin >> x ;

       cout <<"input mouth"<<endl;

       cin >> y ;

       int i;

       long int a[2];

       for(i=0;i<=1;i++)

       {tm.tm_year=x-1900;

       tm.tm_mon=y-1+i;

       tm.tm_mday=1;

       tm.tm_hour=0;

       tm.tm_min=0;

       tm.tm_sec=0;

       tm.tm_isdst=0;

       t_ = mktime(&tm);

       strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);

       cout << t_ <<endl;

       a[i]=t_;

       cout << a[i] << endl;

       }

       fprintf(fp,"a=%d\nb=%d\n",a[0],a[1]);

                           

       fclose(fp);

       system("D:\\yuebao.bat");

       getchar();

       return 0;

       

}

简单地说,通过time函数,将输入的字符串转变为时间后,在转变为unix时间戳,并输入到3.txt中,同时运行yuebao.txt来执行文本的导出

语句解释

  FILE *fp;

       fp=fopen("D:\\3.txt","w");

这两句为定义指针,打开3.txt文件,方式为w

cout <<"inputfirstyear"<<endl;

       cin >> x ;

       cout <<"input mouth"<<endl;

       cin >> y ;

这两句提示输入年和月份,

for(i=0;i<=1;i++)

       {tm.tm_year=x-1900;

       tm.tm_mon=y-1+i;

       tm.tm_mday=1;

       tm.tm_hour=0;

       tm.tm_min=0;

       tm.tm_sec=0;

       tm.tm_isdst=0;

根据time函数的解释,对年和月进行赋值,由于一个月的区间,所以需要计算两次,写了一个for循环。

   t_= mktime(&tm);

       strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);

        cout << t_ <<endl;

引用time函数之类的。t_就是需要的unix时间戳

a[i]=t_;

       cout << a[i] << endl;

       }

       fprintf(fp,"a=%d\nb=%d\n",a[0],a[1]);

                           

       fclose(fp);

       system("D:\\yuebao.bat");

       getchar();

在前面定义了一个数组第一个数值写入a0】表示第一个月,第二个数值写入a1】表示第二个月,同时将值分别赋予ab并通过fp函数导出,通过system文件执行bat的脚本文件,完成导出。

运行方式

         双击time.exe,输入年份和所需要查看的月份之后,就自动导出啦

        

         time.exe所在的文件夹里,可以看到导出的图片