-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit afa28bb
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.o | ||
*.exe | ||
.vscode | ||
/build |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "mongoose"] | ||
path = mongoose | ||
url = https://github.com/cesanta/mongoose.git |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
cmake_minimum_required(VERSION 3.0.0) | ||
project(user_idle_exporter VERSION 0.1) | ||
|
||
option(HIDE_CONSOLE "Hides console window" ON) | ||
set(LISTEN_ADDR "0.0.0.0:9183" CACHE STRING "Listen address") | ||
|
||
if(NOT WIN32) | ||
message(FATAL_ERROR "Only Windows supported") | ||
endif() | ||
|
||
set(BUILD_SHARED_LIBS OFF) | ||
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") | ||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") | ||
|
||
if(HIDE_CONSOLE) | ||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -mwindows") | ||
endif() | ||
|
||
# strip executable | ||
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -s") | ||
|
||
include_directories(mongoose) | ||
add_executable(user_idle_exporter main.c mongoose/mongoose.c) | ||
target_link_libraries(user_idle_exporter ws2_32) | ||
target_compile_definitions(user_idle_exporter PRIVATE | ||
-DUSER_IDLE_EXPORTER_VERSION="${CMAKE_PROJECT_VERSION}" | ||
-DUSER_IDLE_EXPORTER_LISTEN_ADDR="${LISTEN_ADDR}" | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# user_idle_exporter | ||
|
||
[Prometheus](https://prometheus.io) Windows user idle time exporter. Must be run as user, not as system account. | ||
Idle time is a number of seconds since last user interaction (keyboard/mouse). | ||
|
||
Example output: | ||
|
||
``` | ||
# HELP user_exporter_build_info User exporter build info | ||
# TYPE user_exporter_build_info gauge | ||
user_idle_exporter_build_info{version="0.1"} 1 | ||
# HELP user_idle_time User idle time | ||
# TYPE user_idle_time gauge | ||
user_idle_seconds 12 | ||
# HELP user_idle_error User idle time retrieval error | ||
# TYPE user_idle_error gauge | ||
user_idle_error 0 | ||
``` | ||
|
||
Default listen address (0.0.0.0) and port (9183) can be only changed in CMakeLists.txt. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <winsock2.h> | ||
#include <windows.h> | ||
#include <stdio.h> | ||
#include "mongoose.h" | ||
|
||
LASTINPUTINFO lii; | ||
|
||
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_IDLE_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_idle_exporter_build_info{version=\"" USER_IDLE_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_error User idle time retrieval 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, 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_IDLE_EXPORTER_LISTEN_ADDR, fn, &mgr); | ||
printf("Listening " USER_IDLE_EXPORTER_LISTEN_ADDR "\n"); | ||
|
||
for (;;) | ||
mg_mgr_poll(&mgr, 1000); | ||
|
||
return 0; | ||
} | ||
|