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)
|
||||
|
||||
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 =
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -7,3 +7,7 @@ type
|
|||
|
||||
proc defaultStates*(): seq[State] =
|
||||
return @[Success, Failure, Running]
|
||||
|
||||
proc newNotifier*(): Notifier =
|
||||
return Notifier(states: defaultStates())
|
||||
|
||||
|
|
|
@ -4,4 +4,7 @@ import uptimekuma_type
|
|||
export notifier_type
|
||||
export uptimekuma_type
|
||||
|
||||
type
|
||||
Notifiers* = object
|
||||
uptimekuma*: UptimeKuma
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
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