mariogarridopt / xBar-Uptime-Kuma

An xbar plugin to display Uptime Kuma Status on Mac taskbar
GNU General Public License v3.0
26 stars 2 forks source link

API Key usage vs user/password #21

Open kmcrawford opened 1 month ago

kmcrawford commented 1 month ago

I want to allow all of my developers to freely install this, but I don't want them access user/password to kuma. I have made some changes to the code to parse the metrics api that can use the api key. I'll leave this here incase anyone else is looking for this functionality or if we want to include this into this project:

#!/usr/bin/env python3
#
# <xbar.title>UptimeKuma Monitor</xbar.title>
# <xbar.version>v1.1</xbar.version>
# <xbar.author>mariogarridopt</xbar.author>
# <xbar.author.github>mariogarridopt</xbar.author.github>
# <xbar.desc>Show UptimeKuma status</xbar.desc>
# <xbar.dependencies>python</xbar.dependencies>
# <xbar.abouturl>https://github.com/mariogarridopt/xBar-Uptime-Kuma</xbar.abouturl>
# <xbar.image>https://raw.githubusercontent.com/mariogarridopt/xBar-Uptime-Kuma/master/screenshot.png</xbar.image>
#

import os
import requests
import re

# -------------------------------------------
TOKEN = '<API_KEY>'
DOMAIN = 'my.domain.com'
BASE_URL = 'https://' + ':'+ TOKEN + '@' + DOMAIN + '/metrics'
SITE = 'https://' + DOMAIN
REPORTING_URL = 'https://mygrafana.dashboard.com/status?orgId=1&from=now-60m&to=now&&var-Service='
# -------------------------------------------
def parse_line(line, status):
    attributes = {}
    items = re.findall(r'(\w+)=(".*?"|\w+)', line)
    for key, value in items:
        attributes[key] = value.strip('"')
    attributes['status'] = status
    return attributes

def get_response_time(list, name):
    for rs in list:
        if rs['monitor_name'] == name:
            return "- " + rs['status'] + "ms"
    return ""

try:

    x = requests.get(BASE_URL)
    status_pattern = re.compile(r'monitor_status\{(.+?)\} (\d)')
    monitor_rt_pattern = re.compile(r'monitor_response_time\{(.+?)\} (\d+)')

    # Extract matches and parse them
    status_matches = status_pattern.findall(x.text)
    monitor_rt_matches = monitor_rt_pattern.findall(x.text)
    monitor_status_list = [parse_line(match[0], match[1]) for match in status_matches]
    monitor_rt_list = [parse_line(match[0], match[1]) for match in monitor_rt_matches]
    monitor_status_list = [item for item in monitor_status_list if item['monitor_type'] != 'group']

    monitor_total = len(monitor_status_list);
    monitor_online = 0;

    monitor_list_string = [];

    ## GET DATA
    for monitor in monitor_status_list:
        text = monitor['monitor_name'] + " " + get_response_time(monitor_rt_list,monitor['monitor_name']) + " | href='"+ REPORTING_URL + monitor['monitor_name']+"'"
        if(monitor['status']=="1"):
            monitor_online = monitor_online + 1
            text = "🟢 " + text
        elif(monitor['status']=="2"):
            text = "🟡 " + text
        elif(monitor['status']=="3"):
            text = "🔵 " + text
        else:
            text = "❗️ " + text + " color='#ff0000'"
        monitor_list_string.append(text)

    ## PRINT DATA
    if monitor_online == monitor_total:
        print('🌍')
    else:
        print(monitor_online, '/', monitor_total)

    print('---')
    for monitors_text in monitor_list_string:
        print(monitors_text)

    print('---')
    print("Open UptimeKuma | href='" + SITE + "/dashboard'")
except Exception as e:

    print('📛')
    print('---')
    print("No monitor! | color='red'")
    print(e)
    print("Click here to configure | href='file://%s'" % os.path.abspath('uptimekuma.30s.py'))
mariogarridopt commented 1 week ago

This looks good. I can work around it to make an update to the current script.

If you prefer, you can make the changes and setup a pull requests. If you do, we need up update the docs on how to use the script.

I would suggest making the default URL the link to the uptime website.