Store RSS data in Zabbix (external check)

I tried to easily create a mechanism to monitor RSS updates with Zabbix. Because it cannot be implemented by the function of Zabbix alone. I implemented it using Zabbix external check. It would have been nice to output RSS as text and get the difference from the previous time, but when Zabbix server fails and it starts running on another server, the check file is moved. I chose an external check because it was a hassle and I would definitely forget it.

environment

--CentOS 8.0 (Internet access)

What has been achieved

--Get RSS using Python --Get the update from the last time and store it in Zabbix item

Don't talk here

--How to initialize Python --How to install Zabbix --General function description of Zabbix

What I realized with Python

In Python, we got the RSS via the Internet, got the title and update date, and wrote it out on one line. The output is text so that it will eventually be transferred to Zabbix. (If you use the API, you may be able to go in JSON format, but this time I have not done it.) In addition, since I do not want to send duplicate data to Zabbix, I decided to acquire the data of articles updated during the previous acquisition time instead of acquiring all cases.

Regarding the output, Zabbix's external check stores the result of standard output in the item, so it prints with print.

The code is below.

RSS_Checker.py


#!/bin/env python3
import feedparser
from datetime import datetime, timedelta, timezone

#Set the previous period(This time specify the time one hour ago)
lasttime = (datetime.utcnow() - timedelta(hours=1))

#Get RSS
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]) >= lasttime

#Select data using filter function
rdflists = filter(f, feed.entries)

#When there is nothing, the process ends
if not rdflists:
    sys.exit()

#If there is data, output it
for entry in rdflists:
    title = entry.title
    link = entry.link
    time = entry.updated

    print (title, link, time) 

Placement of code on Zabbix server

If you want to use Zabbix external check function, you need to put the script file in the path of ExternalScripts in zabbix_server.conf. The following folders are specified by default.

ExternalScripts=/usr/lib/zabbix/externalscripts

Also, don't forget to set execute permissions and owner settings so that Zabbix users can run it.

# cd /usr/lib/zabbix/externalscripts
# chmod 744 RSS_Checker.py
# chown zabbix:zabbix RSS_Checker.py
# ls -l
-rwxr--r-- 1 zabbix zabbix 1200 Jan 01 01:23 RSS_Checker.py

What you set in Zabbix

Zabbix creates a dedicated Item.

Item 設定画面.jpeg

Select "External check" as the type. Text is selected as the data type. Since the host interface is executed by Zabbix server, specify "127.0.0.1:10051". The monitoring interval must be adjusted to the difference time specified in the code, so it needs to be changed as appropriate. Long code intervals can result in duplicate data storage, and long item monitoring intervals can lead to missed acquisitions.

What didn't work

In this configuration, data was stored in Zabbix Item, but there were two problems.

  1. Even if there are multiple output update information, it will be stored as one record in Zabbix.
  2. An empty record is created even if there is no update information.

There is no particular problem when picking up a character string with Trigger and notifying it, but it is not beautiful. I did some research and saw it, but I couldn't find out about the behavior of the text storage of the external check. The same result was obtained when the data type of Item was "Log" or "String".

I couldn't find a solution to this event, but I was able to capture it, so I decided to complete it.

Hmmm, but still not beautiful. ..

(Continue)

Reference site

-[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

Store RSS data in Zabbix (external check)
Store RSS data in Zabbix (Zabbix sender)
Check for external commands in python
Check the data summary in CASTable
Existence check of external command in Python (like `which`)
Sampling in imbalanced data
Save in Japanese to StringProperty in Google App Engine data store