nix-config/hosts/containers/nncp/collectd_nncp.nim

42 lines
1.4 KiB
Nim

import std/[os, strutils]
import posix
proc getFileSize(path: string): BiggestInt =
var rawInfo: Stat
if stat(path, rawInfo) < 0'i32:
raiseOSError(osLastError(), path)
rawInfo.st_size
let
hostname = getEnv("COLLECTD_HOSTNAME", "localhost")
interval = max(60, getEnv("COLLECTD_INTERVAL", "0").parseFloat.int)
args = commandLineParams()
doAssert(args.len == 1, "pass only the NNCP spool directry as an argument")
proc sumDir(path: string): tuple[size: BiggestInt, packets: int] =
for kind, path in walkDir path:
if kind == pcFile:
result.size = result.size + getFileSize(path)
inc result.packets
while true:
for kind, path in walkDir args[0]:
if kind == pcLinkToDir:
var nodeName = extractFilename path
block:
var rx = sumDir(path / "rx")
stdout.writeLine("""PUTVAL "$#/exec-nncp_$#/bytes-rx" interval=$# N:$#""" %
[hostname, nodeName, $interval, $rx.size])
stdout.writeLine("""PUTVAL "$#/exec-nncp_$#/pending_operations-rx" interval=$# N:$#""" %
[hostname, nodeName, $interval, $rx.packets])
block:
var tx = sumDir(path / "tx")
stdout.writeLine("""PUTVAL "$#/exec-nncp_$#/bytes-tx" interval=$# N:$#""" %
[hostname, nodeName, $interval, $tx.size])
stdout.writeLine("""PUTVAL "$#/exec-nncp_$#/pending_operations-tx" interval=$# N:$#""" %
[hostname, nodeName, $interval, $tx.packets])
flushFile stdout
sleep interval*1000