Last time, I described the process of storing RSS data in Zabbix using Zabbix's external check function, but it is not beautiful and it is different considering that it will be transferred to AWS Lambda etc. in the future. I thought it might be better, so I decided to implement it using Zabbix sender.
Click here for the previous article ⇒ Store RSS data in Zabbix (external check)
--CentOS 8.0 (Internet access)
--Get RSS using Python --Get the update from the last time and store it in Zabbix item
--How to initialize Python --How to install Zabbix --General function description of Zabbix
The flow is basically the same. The only difference is that we are finally running Zabbix sender towards Zabbix.
The code is below.
RSS_Checker.py
#!/bin/env python3
import feedparser
from datetime import datetime, timedelta, timezone
from pyzabbix import ZabbixMetric, ZabbixSender
#Set the previous period(This time specify the time one hour ago)
lasttime = (datetime.utcnow() - timedelta(hours=1))
#RSS URL
RSS_URL = "(Specify URL)"
feed = feedparser.parse(RSS_URL)
#Get articles updated since the last time
def f(entry):
return datetime(*entry.updated_parsed[:6]).date() >= lasttime
#Use the filter function to select data and sort by update time
rdflists = list(filter(f, feed.entries))
rdflists.sort(key=lambda x: x['updated'])
#When there is nothing, the process ends
if not rdflists:
sys.exit()
msgs = []
for entry in rdflists:
title = entry.title
link = entry.link
time = entry.updated
msg = title + ' / ' + link + ' / ' + time
msgs.append(ZabbixMetric('(hostname)', '(key name)', msg))
result = ZabbixSender(use_config=True).send(msgs)
This time, it is executed by cron, not from Zabbix, so please set the location and user according to your environment.
Also, although it is not implemented this time, it can be placed on the server where the code is executed, but please note that some settings on the last line of the code need to be changed.
In Zabbix, create a dedicated Item as before.
This time, specify "Zabbix trapper" as the type. Unlike the last time, Zabbix only waits for processing, so there is no interval specified.
After setting the above and executing it, one update information was successfully stored in one history record! This method can be executed by other than Zabbix server, so it may be used for other purposes.
-[How to get data from rss in Python --Intellectual curiosity](https://intellectual-curiosity.tokyo/2019/03/28/python%E3%81%A7rss%E3%81%8B%E3% 82% 89% E3% 83% 87% E3% 83% BC% E3% 82% BF% E3% 82% 92% E5% 8F% 96% E5% BE% 97% E3% 81% 99% E3% 82% 8B% E6% 96% B9% E6% B3% 95 /) -Analyze RSS and Atom feeds with Python, feedparser --note.nkmk.me
Recommended Posts