This commit is contained in:
osmarks 2021-01-28 20:57:25 +00:00
parent c1138fba8d
commit 1406232927
2 changed files with 21 additions and 9 deletions

View File

@ -11,4 +11,5 @@ bin = @["onstat"]
requires "nim >= 1.4.2" requires "nim >= 1.4.2"
requires "https://github.com/GULPF/tiny_sqlite#8fe760d9" requires "https://github.com/GULPF/tiny_sqlite#8fe760d9"
requires "karax >= 1.2.1" requires "karax >= 1.2.1"
requires "cligen >= 1"

View File

@ -11,15 +11,13 @@ import net
import sequtils import sequtils
import strformat import strformat
import std/exitprocs import std/exitprocs
import cligen
import ./db import ./db
macro includeFile(x: string): string = newStrLitNode(readFile(x.strVal)) macro includeFile(x: string): string = newStrLitNode(readFile(x.strVal))
const css = includeFile("./src/style.css") const css = includeFile("./src/style.css")
let database = openDatabase("./monitoring.sqlite3")
migrate(database)
close(database)
var threadDB {.threadvar.}: Option[DbConn] var threadDB {.threadvar.}: Option[DbConn]
proc getDB(): DbConn {.gcsafe.} = proc getDB(): DbConn {.gcsafe.} =
@ -137,8 +135,21 @@ proc timerCallback(fd: AsyncFD): bool =
asyncCheck pollTargets() asyncCheck pollTargets()
false false
echo "Starting up" proc run(dbPath="./monitoring.sqlite3", port=7800, interval=30000, urls: seq[string]) =
asyncCheck pollTargets() ## Run onstat. Note that the URLs you configure will be persisted in the monitoring database. To remove them, you must manually update this.
addTimer(30000, false, timerCallback) let database = openDatabase(dbPath)
var server = newAsyncHttpServer() migrate(database)
waitFor server.serve(Port(7800), onRequest) 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)"
})