Created
March 25, 2023 14:54
-
-
Save MultiMote/d6d04d84d4b4b6b2499171a7193f6eb8 to your computer and use it in GitHub Desktop.
user idle exporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <winsock2.h> | |
#include <windows.h> | |
#include <stdio.h> | |
#include "mongoose.h" | |
LASTINPUTINFO lii; | |
#define USER_EXPORTER_VERSION "0.0.1" | |
#define USER_EXPORTER_IDLE_TIME_SECONDS (8 * 60) | |
#define USER_EXPORTER_LISTEN_ADDR "http://0.0.0.0:9183" | |
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) | |
{ | |
if (ev == MG_EV_HTTP_MSG) | |
{ | |
struct mg_http_message *hm = (struct mg_http_message *)ev_data; | |
if (mg_http_match_uri(hm, "/")) | |
{ | |
const char html[] = | |
"<html>" | |
"<head><title>user_exporter</title></head>" | |
"<body>" | |
"<h1>user_exporter</h1>" | |
"<p><a href=\"/metrics\">Metrics</a></p>" | |
"<p><i>(version=" USER_EXPORTER_VERSION ")</i></p>" | |
"</body>" | |
"</html>"; | |
mg_http_reply(c, 200, "Content-Type: text/html\r\n", html); | |
} | |
else if (mg_http_match_uri(hm, "/metrics")) | |
{ | |
int ret = GetLastInputInfo(&lii); | |
int seconds = (GetTickCount() - lii.dwTime) / 1000; | |
const char text[] = | |
"# HELP user_exporter_build_info User exporter build info\n" | |
"# TYPE user_exporter_build_info gauge\n" | |
"user_exporter_build_info{version=\"" USER_EXPORTER_VERSION "\"} 1\n" | |
"# HELP user_idle_time User idle time\n" | |
"# TYPE user_idle_time gauge\n" | |
"user_idle_seconds %d\n" | |
"# HELP user_idle User is idle\n" | |
"# TYPE user_idle gauge\n" | |
"user_idle %d\n" | |
"# HELP user_idle_error User idle time retrieve error \n" | |
"# TYPE user_idle_error gauge\n" | |
"user_idle_error %d\n"; | |
mg_http_reply(c, 200, "Content-Type: text/plain\r\n", text, seconds, seconds > USER_EXPORTER_IDLE_TIME_SECONDS, ret == 0); | |
} | |
else | |
{ | |
mg_http_reply(c, 404, NULL, "404\n"); | |
} | |
} | |
} | |
int main() | |
{ | |
lii.cbSize = sizeof(LASTINPUTINFO); | |
struct mg_mgr mgr; | |
mg_mgr_init(&mgr); | |
mg_http_listen(&mgr, USER_EXPORTER_LISTEN_ADDR, fn, &mgr); | |
printf("Listening " USER_EXPORTER_LISTEN_ADDR "\n"); | |
for (;;) | |
mg_mgr_poll(&mgr, 1000); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment