zabbix监控网站黑链接及数据推送
文章目录
- zabbix监控网站黑链接及数据推送
- 1.检测脚本
- 1》编写python脚本
- 2》脚本执行
- 2.数据推送
- 1》方案一
- 2》方案二
- 3.zabbix web
1.检测脚本
1》编写python脚本
创建脚本check_black_links.py,使用python脚本实现网站黑链接检测;
首先,确保你的环境中已经安装了 Python 和 requests
以及 beautifulsoup4
库。没有安装请使用pip安装;
#!/usr/bin/env python3
import requests
from bs4 import BeautifulSoup
import sys
def check_links(url):
try:
response = requests.get(url)
response.raise_for_status()
except requests.exceptions.RequestException as e:
return f"Error accessing {url}: {e}"
soup = BeautifulSoup(response.content, 'html.parser')
bad_links = []
for link in soup.find_all('a', href=True):
link_url = link['href']
if link_url.startswith('#'): # Skip internal page references
continue
if not link_url.startswith(('http://', 'https://')):
link_url = requests.compat.urljoin(url, link_url) # Join relative URLs to base URL
try:
link_response = requests.head(link_url, allow_redirects=True)
if link_response.status_code != 200:
bad_links.append(f'URL: {link_url}, Status Code: {link_response.status_code}')
except requests.exceptions.RequestException as e:
bad_links.append(f'URL: {link_url}, Error: {e}')
if bad_links:
return f"Found bad links: {', '.join(bad_links)}"
else:
return "All links are working properly."
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: check_black_links.py <URL>")
sys.exit(1)
url = sys.argv[1]
result = check_links(url)
print(result)
2》脚本执行
#将脚本上传到服务器
格式:执行脚本语言 脚本 检测网站
python check_black_links.py https://www.cnblogs.com
案例:
# 这里将脚本上传到zabbix的存放脚本目录,检测网站为博客园
python /usr/lib/zabbix/externalscripts/check_black_links.py https://www.cnblogs.com
# 输出如下,这里挑选出部分日志粘贴
Found bad links: URL: https://www.cnblogs.com/cmt/p/18341478, Status Code: 400, URL: https://wz.cnblogs.com/, Status Code: 405, URL: javascript:void(0), Error: No connection adapters were found for 'javascript:void(0)', URL: javascript:void(0), Error: No connection adapters were found for 'javascript:void(0)', URL: javascript:void(0), Error: No connection adapters were found for 'javascript:void(0)', URL: javascript:void(0), Error: No connection adapters were found for 'javascript:void(0)', URL: https://www.cnblogs.com/cmt/articles/18197617, Status Code: 400, URL: https://wz.cnblogs.com/, Status Code: 405, URL: javascript:void(0), Error: No connection adapters were found for 'javascript:void(0)', URL: javascript:void(0), Error: No connection adapters were found for 'javascript:void(0)', URL: javascript:void(0), Error: No connection adapters were found for 'javascript:void(0)', URL: https://www.cnblogs.com/wzh2010/p/18031157, Status Code: 400, URL: https://www.cnblogs.com/Can-daydayup/p/18457705, Status Code: 400, URL: https://www.cnblogs.com/shanyou/p/18457208, Status Code: 400, URL: https://www.cnblogs.com/cmt/p/18453394, Status Code: 400, URL: https://www.cnblogs.com/cmt/p/18453394, Status Code: 400, URL: https://www.cnblogs.com/Ke-Xin-Jsy/p/18458915, Status Code: 400, URL: https://www.cnblogs.com/Ke-Xin-Jsy/p/18458915#commentform, Status Code: 400, URL: javascript:void(0), Error: No connection adapters were found for 'javascript:void(0)', URL: https://www.cnblogs.com/Ke-Xin-Jsy/p/18458915, Status Code: 400, URL: https://www.cnblogs.com/michaelho/p/18458914, Status Code: 400, URL: https://www.cnblogs.com/michaelho/p/18458914#commentform, Status Code: 400
2.数据推送
1》方案一
通过agent推送,适用于执行时间短的脚本推送
# 编辑agent配置文件“zabbix_agentd.conf”,配置UserParameter推送;(注:适合数据小,执行时间短的脚本,执行时间不得大于15s)
vim /etc/zabbix/zabbix_agentd.conf
UserParameter=check.blacklinks[*],/usr/bin/python /usr/lib/zabbix/externalscripts/check_black_links.py $1
# 登录zabbix web页面-->选择监控主机-->创建监控项-->选择监控类型为agent-->选择自定义key(check.blacklinks["https://www.cnblogs.com"])-->选择信息类型为文本-->保存监控项-->最新数据检查数据
2》方案二
通过zabbix sender推送,适用于执行时间长的脚本推送
# 编写执行推送脚本,可通过crontab来定期推送数据(注:数据大小不定,执行时间长的脚本,执行时间大于15s可选择此方式)
# 登录zabbix web页面-->选择监控主机-->创建监控项-->选择监控类型为trapper-->选择自定义key(zbx.sender)-->选择信息类型为文本-->保存监控项-->最新数据检查数据
创建推送数据脚本zabbix_sender_check_black_links.sh
#!/bin/bash
# Zabbix server details
ZABBIX_SERVER="10.11.12.13"
ZABBIX_PORT="10051"
ZABBIX_HOST="Zabbix server test"
ZABBIX_KEY="zbx.sender"
# Call the Python script and get the output
BLACK_LINKS_COUNT=$(python /usr/lib/zabbix/externalscripts/check_black_links.py https://www.cnblogs.com)
# Use zabbix-sender to send the data to Zabbix server
/usr/bin/zabbix_sender -z "$ZABBIX_SERVER" -p "$ZABBIX_PORT" -s "$ZABBIX_HOST" -k "$ZABBIX_KEY" -o "$BLACK_LINKS_COUNT" >/dev/null
crontab编写,定期执行脚本推送数据
# 登录服务器定期执行脚本
crontab -l
1 * * * * /usr/bin/bash /usr/lib/zabbix/externalscripts/zabbix_sender_check_black_links.sh
3.zabbix web
最新推送检测数据