diff --git a/roles/copr/frontend/files/monitoring.py b/roles/copr/frontend/files/monitoring.py new file mode 100644 index 0000000000..5c264bddf2 --- /dev/null +++ b/roles/copr/frontend/files/monitoring.py @@ -0,0 +1,49 @@ +#! /usr/bin/python3 + +import requests +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 __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) + + 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) + + +if __name__ == '__main__': + copr_cdn_url = "?t1=1641301793&t2=1641388193&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=last31days&backtrack=4" + copr_ping_url = "?t1=1628506451&t2=1629111251&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=last31days&backtrack=4" + collect(copr_cdn_url, "copr_cdn_status", "Copr's CDN status", "td.serviceOK:nth-child(4)", + "/var/lib/node_exporter/textfile_collector/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/node_exporter/textfile_collector/copr_ping_status.prom.new") diff --git a/roles/copr/frontend/tasks/main.yml b/roles/copr/frontend/tasks/main.yml index a1f923068e..99926b1e62 100644 --- a/roles/copr/frontend/tasks/main.yml +++ b/roles/copr/frontend/tasks/main.yml @@ -29,6 +29,53 @@ tags: - packages +- name: Configure cron job for generating prometheus metrics hourly + when: production|bool + cron: + name: "generating prometheus metrics" + user: root + minute: 0 + job: /usr/bin/python3 monitoring.py + tags: + - cron_tasks + +- name: Deploy the prometheus monitoring script + when: production|bool + copy: + src: monitoring.py + dest: /usr/bin/monitoring.py + mode: 0750 + owner: root + group: root + tags: + - cron_tasks + +- name: Check that file copr_cdn_status.prom.new exists + when: production|bool + stat: path=/var/lib/node_exporter/textfile_collector/copr_cdn_status.prom.new + register: copr_cdn_status_stat + tags: + - cron_tasks + +- name: Rename copr_cdn_status.prom.new to copr_cdn_status.prom + command: mv /var/lib/node_exporter/textfile_collector/copr_cdn_status.prom.new /var/lib/node_exporter/textfile_collector/copr_cdn_status.prom + when: copr_cdn_status_stat.stat.exists and production|bool + tags: + - cron_tasks + +- name: Check that file copr_ping_status.prom.new exists + when: production|bool + stat: path=/var/lib/node_exporter/textfile_collector/copr_ping_status.prom.new + register: copr_ping_status_stat + tags: + - cron_tasks + +- name: Rename copr_ping_status.prom.new to copr_ping_status.prom + command: mv /var/lib/node_exporter/textfile_collector/copr_ping_status.prom.new /var/lib/node_exporter/textfile_collector/copr_ping_status.prom + when: copr_ping_status_stat.stat.exists and production|bool + tags: + - cron_tasks + # we install python-alembic because https://bugzilla.redhat.com/show_bug.cgi?id=1536058 - name: install additional pkgs for copr-frontend dnf: @@ -41,6 +88,7 @@ - python3-alembic - mod_auth_gssapi - nagios-plugins-http + - python3-prometheus_client tags: - packages