From 14062329276f4a29bc07b3302432ccb378b5409c Mon Sep 17 00:00:00 2001 From: osmarks Date: Thu, 28 Jan 2021 20:57:25 +0000 Subject: [PATCH] CLI --- onstat.nimble | 3 ++- src/onstat.nim | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/onstat.nimble b/onstat.nimble index 10d0ab4..cd36092 100644 --- a/onstat.nimble +++ b/onstat.nimble @@ -11,4 +11,5 @@ bin = @["onstat"] requires "nim >= 1.4.2" requires "https://github.com/GULPF/tiny_sqlite#8fe760d9" -requires "karax >= 1.2.1" \ No newline at end of file +requires "karax >= 1.2.1" +requires "cligen >= 1" \ No newline at end of file diff --git a/src/onstat.nim b/src/onstat.nim index 8defb8c..2f4df27 100644 --- a/src/onstat.nim +++ b/src/onstat.nim @@ -11,15 +11,13 @@ import net import sequtils import strformat import std/exitprocs +import cligen import ./db macro includeFile(x: string): string = newStrLitNode(readFile(x.strVal)) const css = includeFile("./src/style.css") -let database = openDatabase("./monitoring.sqlite3") -migrate(database) -close(database) var threadDB {.threadvar.}: Option[DbConn] proc getDB(): DbConn {.gcsafe.} = @@ -137,8 +135,21 @@ proc timerCallback(fd: AsyncFD): bool = asyncCheck pollTargets() false -echo "Starting up" -asyncCheck pollTargets() -addTimer(30000, false, timerCallback) -var server = newAsyncHttpServer() -waitFor server.serve(Port(7800), onRequest) +proc run(dbPath="./monitoring.sqlite3", port=7800, interval=30000, urls: seq[string]) = + ## Run onstat. Note that the URLs you configure will be persisted in the monitoring database. To remove them, you must manually update this. + let database = openDatabase(dbPath) + migrate(database) + for url in urls: + echo &"Adding {url}" + database.exec("INSERT INTO sites (url) VALUES (?)", url) + close(database) + echo "Starting up" + asyncCheck pollTargets() + addTimer(interval, false, timerCallback) + var server = newAsyncHttpServer() + waitFor server.serve(Port(port), onRequest) +dispatch(run, help={ + "dbPath": "path to SQLite3 database for historical data logging", + "port": "port to serve HTTP on", + "interval": "interval at which to poll other services (milliseconds)" +}) \ No newline at end of file