当前位置:网站首页>使用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适配的字体问题上,很是无奈啊,居然还要考虑字体的问题,阿里云的服务器也是真够能偷懒的。
边栏推荐
- 2021-08-13和鹏哥学C语言-数组
- (转载)ArcGIS Engine中各种点的创建方法
- 英语基础句型结构------起源
- list升序和降序
- Analysis of the transaction problem of chained method call
- 鹏哥C语言第四课(3)
- Anaconda is used on vscode (the environment has been configured)
- 智能合约dapp系统开发流程技术
- MFC中0x003b66c3 处有未经处理的异常: 0xC000041D: 用户回调期间遇到未经处理的异常
- Zongzi battle - guess who can win
猜你喜欢

文案秘籍七步曲至----文献团队协作管理

RT thread learning notes (V) -- edit, download and debug programs

从蚂蚁的觅食过程看团队研发(转载)

35. 搜索插入位置

sigmod 函数与softmax 函数对比

抽象工厂及其改进示例

Issue 7: how do you choose between curling up and lying flat

解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。

Issue 6: which mainstream programming language should college students choose

el-table实现可编辑表格
随机推荐
c 语言中宏参数的字符串化跟宏参数的连接
Flutter CachedNetworkImage圆角
Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
242.有效的字母异位词
从蚂蚁的觅食过程看团队研发(转载)
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
RT-Thread 学习笔记(五)---编辑、下载、调试程序
flutter 背景变灰效果,如何透明度,灰色蒙板遮罩
Flutter集成极光推送
Analysis of the transaction problem of chained method call
[leetcode daily question 2021/4/29]403. Frogs cross the river
Sql Server 数据库之数据类型
Flutter jni混淆 引入.so文件release包闪退
display-inline+calc实现左中右布局,中间自适应
json_ object_ put: Assertion `jso->_ ref_ count > 0‘ failed. Aborted (core dumped)
Zongzi battle - guess who can win
剑指Offer(二十):包含min函数的栈
MFC图片控件
【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)
Sql Server 数据库之完整性约束