当前位置:网站首页>使用Selenium抓取zabbix性能监控图
使用Selenium抓取zabbix性能监控图
2022-07-26 10:45:00 【软件测试木木】
概要
老样子先来个概要,因为公司一直都子使用企业微信,前两天刚刚将所有zabbix的告警信息接到微信上,总感觉还差那么一点,就想着就高等级的告警加上一个性能监控图,这样只看告警信息大概就清楚问题了,不用在登录到zabbix再去查找信息,就因为这个想法让我掉坑里呆了一整天,蓦然回首还是自己太菜啊,下面先贴出来使用Python登录zabbix并且获取到对应告警项监控图的代码,之后完整代码会贴到github中,链接放到简书上~
实现抓取
以下几点基础:
1、告警信息的发送message里,可以发送zabbix的宏变量Item ID信息。
2、根据Item ID,可以直接使用特殊URL携带Item ID信息获取对应监控截图。
以下为获取截图完整代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# name image.py
from selenium import webdriver
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def get_item_graph(itemid,flag,eventid):
temp_name = "/tmp/"+eventid+".png"
#save_screenshot仅能保存png格式图片,所以文件名定义需要以png结尾
driver = webdriver.PhantomJS("
/usr/local/zabbix-agent-ops/phantomjs-2.1.1/bin/phantomjs",service_log_path=os.path.devnull)
#使用PhantomJS可以模拟浏览器进行访问
driver.get("http://127.0.0.1/zabbix/")
driver.set_window_size(640,480)
driver.find_element_by_id("name").send_keys("armo")
driver.find_element_by_id("password").send_keys("123456")
driver.find_element_by_id("enter").click()
#模拟访问url,在对应的元素element处输入用户名密码后click登陆
if flag:
driver.get("
http://127.0.0.1/zabbix/history.php?action=showgraph&fullscreen=1&itemids[]="+itemid)
else:
driver.get("
http://127.0.0.1/zabbix/history.php?action=showvalues&fullscreen=1&itemids[]="+itemid)
#flag如果是1,则对应Item有对应的graph获取;如果是0,则获取最新一段时间的值
driver.save_screenshot(temp_name)
#将网页内容保存为png图片
driver.close()
driver.quit()
if __name__ == "__main__":
if len(sys.argv) > 1:
itemid = sys.argv[1] #脚本传递的第一个参数 Item ID
flag = sys.argv[2] #脚本传递的第二个参数 Flag,从zabbix数据库item和graph的对应表查询item是否具有对应的graph,如果有则传递1到脚本,无传递0
eventid = sys.argv[3] #脚本传递的第三个参数 告警信息的Event ID,用来命名png图片
get_item_graph(itemid,flag,eventid)
#以上三个参数为前置调用脚本分析zabbix的发送信息后得到,之后会更新在后续文章中
遇到的问题
1、第一个session和cookies的保存问题,如果登录之后不携带cookies是无法访问对应监控页面的,刚开始选择requests模块,感觉相对较难,对于刚入门的我来说还是selenium比较合适,不用考虑session和cookies的问题。
2、脚本在测试环境运行非常好,截图信息也很准确,但是到线上服务器就出了问题。
很明显线上环境返回的图,前面一段居然是乱码。检查了所有环境,最后才发现问题出在线上服务器没有安装和zabbix适配的字体问题上,很是无奈啊,居然还要考虑字体的问题,阿里云的服务器也是真够能偷懒的。
边栏推荐
- 剑指Offer(五):用两个栈实现队列
- Flutter CachedNetworkImage圆角
- mysql20210906
- 使用float实现左中右布局,中间内容自适应
- Add touch screen driver for stemwin 5.22 on Shenzhou IV development board
- Anaconda is used on vscode (the environment has been configured)
- 第5期:大学生入职必备技能之二
- Error[pe147]: declaration is incompatible with 'error problem
- The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
- (转载)ArcGIS Engine中各种点的创建方法
猜你喜欢
在神州IV开发板上为STemWin 5.22加入触屏驱动
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
Disable usbjatg in Altium Designer
mysql20210906
英语基础句型结构------起源
第6期:大学生应该选择哪种主流编程语言
Analysis of the transaction problem of chained method call
[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)
RT-Thread 学习笔记(五)---编辑、下载、调试程序
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
随机推荐
英语基础句型结构------起源
[paper after dinner] deep mining external perfect data for chestx ray disease screening
Flutter TextField设置高度并且自动换行,圆角边框去除下划线
$router和$route的区别
RT-Thread 学习笔记(三)---用SCons 构建编译环境
Sql Server 之SQL语句对基本表及其中的数据的创建和修改
Common classes (understand)
Zongzi battle - guess who can win
Sql Server 数据库之完整性约束
使用Geoprocessor 工具
Issue 6: which mainstream programming language should college students choose
用两个栈实现队列
7-25 0-1背包 (50分)
Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
字典与int矩阵
242.有效的字母异位词
11 handle "self assignment" in operator=
Flutter集成极光推送
Sql Server 数据库之数据类型
C language callback function