diff --git a/nim.cfg b/nim.cfg new file mode 100644 index 0000000..521e21d --- /dev/null +++ b/nim.cfg @@ -0,0 +1 @@ +-d:ssl diff --git a/norg/borg/borg.nim b/norg/borg/borg.nim index e9e123b..c631569 100644 --- a/norg/borg/borg.nim +++ b/norg/borg/borg.nim @@ -41,19 +41,20 @@ proc initRepo(nc: NorgConfig, repo: Repository): int = return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others) proc backupSources(nc: NorgConfig, repo: Repository): int = - if nc.notifiers.len > 0: - notify(nc.notifiers, state=Running) + let start_time = now() + notify(nc.notifiers, state=Running) let others = concat(nc.source_dirs, nc.args.others) let archivename = repo.path & "::" & genArchiveName() let res = run genCommand(cmd = "create", repo = archivename, others = others) - if nc.notifiers.len > 0: - case res - of 0: - notify(nc.notifiers, state=Success) - of 1: - notify(nc.notifiers, state=Failure) - else: - notify(nc.notifiers, state=Failure) + let end_time = now() + let elapsed = (end_time - start_time) + let total = elapsed.inMilliSeconds() + of 0: + notify(nc.notifiers, state=Success, runtime=total) + of 1: + notify(nc.notifiers, state=Failure, runtime=total) + else: + notify(nc.notifiers, state=Failure, runtime=total) proc listArchives(nc: NorgConfig, repo: Repository): int = diff --git a/norg/config/notifier_config.nim b/norg/config/notifier_config.nim index 019522d..521c87b 100644 --- a/norg/config/notifier_config.nim +++ b/norg/config/notifier_config.nim @@ -13,12 +13,10 @@ proc parseUptimeKumaConf(conf: TomlValueRef): UptimeKuma = uk.states = defaultStates() return uk -proc parseNotifiers*(in_conf: TomlValueRef): seq[Notifier] = - var notifiers: seq[Notifier] = @[] +proc parseNotifiers*(in_conf: TomlValueRef): Notifiers = + var notifiers = Notifiers() if in_conf.hasKey("uptimekuma"): let u = parseUptimeKumaConf(in_conf["uptimekuma"]) - echo u - notifiers.add(u) - echo notifiers[0] - + notifiers.uptimekuma = u + echo notifiers return notifiers diff --git a/norg/model/config_type.nim b/norg/model/config_type.nim index 4f54ff2..70bd19f 100644 --- a/norg/model/config_type.nim +++ b/norg/model/config_type.nim @@ -14,7 +14,7 @@ type keep_weekly*: int keep_monthly*: int #checks*: Check - see borgmatic - notifiers*: seq[Notifier] + notifiers*: Notifiers args*: NorgArgs encryption_password: string diff --git a/norg/model/notifier_type.nim b/norg/model/notifier_type.nim index dbc39d2..381dfa7 100644 --- a/norg/model/notifier_type.nim +++ b/norg/model/notifier_type.nim @@ -7,3 +7,7 @@ type proc defaultStates*(): seq[State] = return @[Success, Failure, Running] + +proc newNotifier*(): Notifier = + return Notifier(states: defaultStates()) + diff --git a/norg/model/notifier_types.nim b/norg/model/notifier_types.nim index 23156df..d5aa29e 100644 --- a/norg/model/notifier_types.nim +++ b/norg/model/notifier_types.nim @@ -4,4 +4,7 @@ import uptimekuma_type export notifier_type export uptimekuma_type +type + Notifiers* = object + uptimekuma*: UptimeKuma diff --git a/norg/model/uptimekuma_type.nim b/norg/model/uptimekuma_type.nim index c41e2aa..f82c1c8 100644 --- a/norg/model/uptimekuma_type.nim +++ b/norg/model/uptimekuma_type.nim @@ -1,10 +1,25 @@ import state_type import notifier_type +import strformat -type +import ../utils/httprequest + +type UptimeKuma* = object of Notifier - extra*: int -proc send_notify*(uk: Notifier, state: State): int {.discardable.} = - echo "Would send to UptimeKuma here" +proc send_notify*(uk: UptimeKuma, state: State, runtime: int = 0): int {.discardable.} = + 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 diff --git a/norg/notifier/notifier.nim b/norg/notifier/notifier.nim index b418fc6..0cac445 100644 --- a/norg/notifier/notifier.nim +++ b/norg/notifier/notifier.nim @@ -1,8 +1,7 @@ import ../model/state_type import ../model/notifier_types -proc notify*(notifiers: seq[Notifier], state: State): int {.discardable.} = - for n in notifiers: - if state in n.states: - echo "Sending notification to " & n.base_url - n.send_notify(state) +proc notify*(notifiers: Notifiers, state: State, runtime: int = 0): int {.discardable.} = + if notifiers.uptimekuma.base_url != "" and + state in notifiers.uptimekuma.states: + notifiers.uptimekuma.send_notify(state, runtime) diff --git a/norg/utils/httprequest.nim b/norg/utils/httprequest.nim new file mode 100644 index 0000000..1fee95a --- /dev/null +++ b/norg/utils/httprequest.nim @@ -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 + +