add working uptime kuma notifier

This commit is contained in:
Paul Wilde 2024-08-18 15:12:44 +01:00
parent 6de02054f7
commit c659fe3212
9 changed files with 67 additions and 26 deletions

1
nim.cfg Normal file
View file

@ -0,0 +1 @@
-d:ssl

View file

@ -41,19 +41,20 @@ proc initRepo(nc: NorgConfig, repo: Repository): int =
return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others) return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others)
proc backupSources(nc: NorgConfig, repo: Repository): int = proc backupSources(nc: NorgConfig, repo: Repository): int =
if nc.notifiers.len > 0: let start_time = now()
notify(nc.notifiers, state=Running) notify(nc.notifiers, state=Running)
let others = concat(nc.source_dirs, nc.args.others) let others = concat(nc.source_dirs, nc.args.others)
let archivename = repo.path & "::" & genArchiveName() let archivename = repo.path & "::" & genArchiveName()
let res = run genCommand(cmd = "create", repo = archivename, others = others) let res = run genCommand(cmd = "create", repo = archivename, others = others)
if nc.notifiers.len > 0: let end_time = now()
case res let elapsed = (end_time - start_time)
of 0: let total = elapsed.inMilliSeconds()
notify(nc.notifiers, state=Success) of 0:
of 1: notify(nc.notifiers, state=Success, runtime=total)
notify(nc.notifiers, state=Failure) of 1:
else: notify(nc.notifiers, state=Failure, runtime=total)
notify(nc.notifiers, state=Failure) else:
notify(nc.notifiers, state=Failure, runtime=total)
proc listArchives(nc: NorgConfig, repo: Repository): int = proc listArchives(nc: NorgConfig, repo: Repository): int =

View file

@ -13,12 +13,10 @@ proc parseUptimeKumaConf(conf: TomlValueRef): UptimeKuma =
uk.states = defaultStates() uk.states = defaultStates()
return uk return uk
proc parseNotifiers*(in_conf: TomlValueRef): seq[Notifier] = proc parseNotifiers*(in_conf: TomlValueRef): Notifiers =
var notifiers: seq[Notifier] = @[] var notifiers = Notifiers()
if in_conf.hasKey("uptimekuma"): if in_conf.hasKey("uptimekuma"):
let u = parseUptimeKumaConf(in_conf["uptimekuma"]) let u = parseUptimeKumaConf(in_conf["uptimekuma"])
echo u notifiers.uptimekuma = u
notifiers.add(u) echo notifiers
echo notifiers[0]
return notifiers return notifiers

View file

@ -14,7 +14,7 @@ type
keep_weekly*: int keep_weekly*: int
keep_monthly*: int keep_monthly*: int
#checks*: Check - see borgmatic #checks*: Check - see borgmatic
notifiers*: seq[Notifier] notifiers*: Notifiers
args*: NorgArgs args*: NorgArgs
encryption_password: string encryption_password: string

View file

@ -7,3 +7,7 @@ type
proc defaultStates*(): seq[State] = proc defaultStates*(): seq[State] =
return @[Success, Failure, Running] return @[Success, Failure, Running]
proc newNotifier*(): Notifier =
return Notifier(states: defaultStates())

View file

@ -4,4 +4,7 @@ import uptimekuma_type
export notifier_type export notifier_type
export uptimekuma_type export uptimekuma_type
type
Notifiers* = object
uptimekuma*: UptimeKuma

View file

@ -1,10 +1,25 @@
import state_type import state_type
import notifier_type import notifier_type
import strformat
type import ../utils/httprequest
type
UptimeKuma* = object of Notifier UptimeKuma* = object of Notifier
extra*: int
proc send_notify*(uk: Notifier, state: State): int {.discardable.} = proc send_notify*(uk: UptimeKuma, state: State, runtime: int = 0): int {.discardable.} =
echo "Would send to UptimeKuma here" var status: string
case state
of Success, Running:
status = "up"
else:
status = "down"
let url = fmt"{uk.base_url}?status={status}&msg={state}&ping={runtime}"
echo "Sending notification to " & url
let res = sendHttpRequest(HttpGet, url)
if res.status == $Http200:
return 0
else:
return 1

View file

@ -1,8 +1,7 @@
import ../model/state_type import ../model/state_type
import ../model/notifier_types import ../model/notifier_types
proc notify*(notifiers: seq[Notifier], state: State): int {.discardable.} = proc notify*(notifiers: Notifiers, state: State, runtime: int = 0): int {.discardable.} =
for n in notifiers: if notifiers.uptimekuma.base_url != "" and
if state in n.states: state in notifiers.uptimekuma.states:
echo "Sending notification to " & n.base_url notifiers.uptimekuma.send_notify(state, runtime)
n.send_notify(state)

View file

@ -0,0 +1,20 @@
import httpclient
export httpclient
proc sendHttpRequest*(meth: HttpMethod, url: string, body: string = "", headers: HttpHeaders = newHttpHeaders(), timeout: int = 10): Response =
let client = newHttpClient(headers = headers, timeout = timeout * 1000)
var res: Response
try:
case meth
of HttpGET:
res = client.get(url)
of HttpPost:
res = client.post(url, body)
else:
discard
finally:
client.close()
return res