add working uptime kuma notifier
This commit is contained in:
parent
6de02054f7
commit
c659fe3212
9 changed files with 67 additions and 26 deletions
1
nim.cfg
Normal file
1
nim.cfg
Normal file
|
@ -0,0 +1 @@
|
||||||
|
-d:ssl
|
|
@ -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)
|
||||||
|
let total = elapsed.inMilliSeconds()
|
||||||
of 0:
|
of 0:
|
||||||
notify(nc.notifiers, state=Success)
|
notify(nc.notifiers, state=Success, runtime=total)
|
||||||
of 1:
|
of 1:
|
||||||
notify(nc.notifiers, state=Failure)
|
notify(nc.notifiers, state=Failure, runtime=total)
|
||||||
else:
|
else:
|
||||||
notify(nc.notifiers, state=Failure)
|
notify(nc.notifiers, state=Failure, runtime=total)
|
||||||
|
|
||||||
|
|
||||||
proc listArchives(nc: NorgConfig, repo: Repository): int =
|
proc listArchives(nc: NorgConfig, repo: Repository): int =
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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())
|
||||||
|
|
||||||
|
|
|
@ -4,4 +4,7 @@ import uptimekuma_type
|
||||||
export notifier_type
|
export notifier_type
|
||||||
export uptimekuma_type
|
export uptimekuma_type
|
||||||
|
|
||||||
|
type
|
||||||
|
Notifiers* = object
|
||||||
|
uptimekuma*: UptimeKuma
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,25 @@
|
||||||
import state_type
|
import state_type
|
||||||
import notifier_type
|
import notifier_type
|
||||||
|
import strformat
|
||||||
|
|
||||||
|
import ../utils/httprequest
|
||||||
|
|
||||||
type
|
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
|
||||||
|
|
||||||
|
|
|
@ -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)
|
|
||||||
|
|
20
norg/utils/httprequest.nim
Normal file
20
norg/utils/httprequest.nim
Normal 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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue