copr-fe: prometheus monitoring to gather the actual state

Not the "historical" info which is calculated into the percentage
of time service spent in UP state.
This commit is contained in:
Pavel Raiskup 2023-02-10 14:40:39 +01:00
parent 39070dc595
commit cacaec9c8e

View file

@ -5,45 +5,35 @@ from bs4 import BeautifulSoup
from prometheus_client import CollectorRegistry, write_to_textfile, Gauge
class TextfileCollector:
NAGIOS_URL = 'https://nagios.fedoraproject.org/nagios/cgi-bin//avail.cgi'
def collect_nagios_service_state(url, name, documentation, filename):
registry = CollectorRegistry()
gauge = Gauge(name, documentation, registry=registry)
state = 0
def __init__(self, url, name, documentation, css_selector, filename):
self.registry = CollectorRegistry()
self.url = url
self.css_selector = css_selector
self.filename = filename
self.copr_cdn_status = Gauge(name, documentation, registry=self.registry)
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, features="lxml")
if soup.select_one("div.serviceOK"):
state = 1
except Exception:
pass
def collect_data(self):
url = self.NAGIOS_URL + self.url
r = requests.get(url)
html = r.text
soup = BeautifulSoup(html, features="lxml")
service_ok = soup.select_one(self.css_selector)
return service_ok.text.strip('%')
def set(self, data):
self.copr_cdn_status.set(data)
write_to_textfile(self.filename, self.registry)
def collect(url, name, documentation, css_selector, filename):
textfile_collector = TextfileCollector(url, name, documentation, css_selector, filename)
percentage = textfile_collector.collect_data()
textfile_collector.set(percentage)
gauge.set(state)
write_to_textfile(filename, registry)
if __name__ == '__main__':
copr_cdn_url = "?show_log_entries=&full_log_entries=&" \
"host=copr-fe.aws.fedoraproject.org&service=The+copr+cdn+status&assumeinitialstates=yes&" \
"assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&" \
"initialassumedhoststate=0&initialassumedservicestate=0&timeperiod=last7days&backtrack=4"
copr_ping_url = "?show_log_entries=&host=copr-be.aws.fedoraproject.org&" \
"service=The+copr-ping+package+builds&assumeinitialstates=yes&assumestateretention=yes&" \
"assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedhoststate=0&" \
"initialassumedservicestate=0&timeperiod=last7days&backtrack=4"
collect(copr_cdn_url, "copr_cdn_status", "Copr's CDN status", "td.serviceOK:nth-child(4)",
"/var/lib/prometheus/node-exporter/copr_cdn_status.prom.new")
collect(copr_ping_url, "copr_ping_status", "Status of build of copr-ping package", "td.serviceOK:nth-child(4)",
"/var/lib/prometheus/node-exporter/copr_ping_status.prom.new")
collect_nagios_service_state(
"https://nagios.fedoraproject.org/nagios/cgi-bin/extinfo.cgi?type=2&host=copr-fe.aws.fedoraproject.org&service=The+copr+cdn+status",
"fedora_copr_cdn_up",
"1 if Nagios reports that AWS CloudFront CDN for Fedora Copr works fine",
"/var/lib/prometheus/node-exporter/fedora_copr_cdn_up.prom",
)
collect_nagios_service_state(
"https://nagios.fedoraproject.org/nagios/cgi-bin/extinfo.cgi?type=2&host=copr-be.aws.fedoraproject.org&service=The+copr-ping+package+builds",
"fedora_copr_ping_package_ok",
"1 if Nagios reports that the \"ping\" package builds fine in Fedora Copr",
"/var/lib/prometheus/node-exporter/fedora_copr_ping_package_ok.prom",
)