From 9eb61b166afa596b1042adc37f8020b07308c611 Mon Sep 17 00:00:00 2001 From: ahtlon Date: Sat, 25 Oct 2025 21:51:14 +0200 Subject: [PATCH 1/3] Add printer scraping --- machines/overwatch/configuration.nix | 9 ++ machines/overwatch/printer_module.nix | 33 +++++++ machines/overwatch/pull_info.sh | 129 ++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 machines/overwatch/printer_module.nix create mode 100644 machines/overwatch/pull_info.sh diff --git a/machines/overwatch/configuration.nix b/machines/overwatch/configuration.nix index aa2d89e..d6ece49 100644 --- a/machines/overwatch/configuration.nix +++ b/machines/overwatch/configuration.nix @@ -12,6 +12,7 @@ with lib; self.nixosModules.malobeo.metrics ../modules/malobeo_user.nix ../modules/sshd.nix + ./printer_module.nix ]; networking.firewall.allowedTCPPorts = [ 80 3100 ]; @@ -77,6 +78,8 @@ with lib; }; }; }; + + printer_scraping.enable = true; services.prometheus = { enable = true; @@ -89,6 +92,12 @@ with lib; targets = [ "127.0.0.1:9002" ]; }]; } + { + job_name = "printer"; + static_configs = [{ + targets = [ "127.0.0.1:9091" ]; + }]; + } { job_name = "durruti"; static_configs = [{ diff --git a/machines/overwatch/printer_module.nix b/machines/overwatch/printer_module.nix new file mode 100644 index 0000000..8d457ff --- /dev/null +++ b/machines/overwatch/printer_module.nix @@ -0,0 +1,33 @@ +{config, lib, pkgs, ...}: +{ + options.printer_scraping = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Enable the script to pull data from the printer" + }; + timer = lib.mkOption { + type = lib.types.string; + default = "1m"; + description = "systemd timer for script execution"; + }; + }; + + config = lib.mkIf config.printer_scraping.enable { + systemd.services."printer-scraping" = { + description = "Pull printer stats and upload to influxdb"; + serviceConfig.Type = "oneshot"; + path = with pkgs; [yq curl]; + script = "exec ${./pull_info.sh}"; + }; + systemd.timers."printer-scraping" = { + wantedBy = ["timers.target"]; + timerConfig = { + OnBootSec = "60s"; + OnUnitActiveSec = config.printer_scraping.timer; + Unit = "printer-scraping.service"; + }; + }; + services.prometheus.pushgateway.enable = true; #Im not dealing with influx + }; +} \ No newline at end of file diff --git a/machines/overwatch/pull_info.sh b/machines/overwatch/pull_info.sh new file mode 100644 index 0000000..37244b3 --- /dev/null +++ b/machines/overwatch/pull_info.sh @@ -0,0 +1,129 @@ +#!/usr/bin/env bash +set -o pipefail + +#Functions--------------- +get_cookie () { + if [[ $1 == "-d" ]]; then + cookie=$(cat request_example_1.txt) + else + cookie=$(curl -D - -X GET http://192.168.1.42/wcd/index.html) + fi + + exitCode="$?" + if [[ $exitCode == "7" ]]; + then + echo "Server offline" + exit 0 + elif [[ $exitCode != "0" ]]; + then + echo "Something went wrong" + exit 1 + fi + + cookie=$(echo "$cookie" | grep Set-Cookie | grep -oP "ID=\K[^.]+" ) + if [[ $cookie == "" ]] + then + echo "No cookie got!" + exit 1 + fi +} +get_values () { + local path="$1" + local -n keys=$2 + local name="$3" + + local_system_counter_data=$(echo "$system_counter_data" | jq "$path | .[]") + for key in "${keys[@]}"; + do + value=$(echo "$local_system_counter_data" | + jq "select(.Type==\"$key\") | .Count" | + sed 's/"//g' + ) + valueStore=$(echo "$valueStore"; echo "$name"_"$key" "$value") + done +} +get_values_DeviceStatus () { + local -n keys=$1 + local name="$2" + + local_system_counter_data=$(echo "$system_counter_data" | jq ".MFP.Common.DeviceStatus") + for key in "${keys[@]}"; + do + value=$(echo "$local_system_counter_data" | + jq ".$key" | + sed 's/"//g' + ) + valueStore=$(echo "$valueStore"; echo "$name"_"$key" "$value") + done + +} +get_values_consumables () { + local -n keys=$1 + local name="$2" + + local_system_consumables_data=$(echo "$system_consumables_data" | jq ".[] |.DeviceInfo.ConsumableList.Consumable | .[]") + for key in "${keys[@]}"; + do + value=$( + echo "$local_system_consumables_data" | + jq "select(.Name==\"$key\") | .CurrentLevel.LevelPer" | + sed 's/"//g' + ) + valueStore=$(echo "$valueStore"; echo "$name"_"${key//[^a-zA-Z_-]/_}" "$value") + done +} +#End Functions---------- + +#Variables----------------------- +system_counter_DeviceStatus_keys=("ScanStatus" "PrintStatus" "Processing" "NetworkErrorStatus" "KmSaasgw" "HddMirroringErrorStatus") +system_counter_TotalCounter_keys=("Total" "DuplexTotal" "Document" "Paper" "TotalLarge" "PrintPageTotal" "PaperSizeA3" "PaperSizeA4" "PaperSizeB4" "PaperSizeB5" "PaperSizeOther" "Nin12in1" "PaperTypeNormal" "PaperTypeOther") +system_counter_FullColorCounter_keys=("PrintPageTotal" "A3" "A4" "B4" "B5" "Other") +system_counter_BlackCounter_keys=("PrintPageTotal" "A3" "A4" "B4" "B5" "Other") +system_counter_DoubleColorCounter_keys=("PrintPageTotal" "A3" "A4" "B4" "B5" "Other") +system_counter_CopyCounter_keys=("BwTotal" "FullColorTotal" "Total" "BwLarge" "FullColorLarge" "BiColorLarge") +system_counter_PrintCounter_keys=("BwTotal" "FullColorTotal" "BiColorTotal" "Total" "BwLarge" "FullColorLarge" "BiColorLarge") +system_counter_ScanFaxCounter_keys=("DocumentReadTotal" "DocumentReadLarge" "FaxReceive" "FaxSend") +system_consumables_base_keys=("Toner (Yellow)" "Toner (Magenta)" "Toner (Cyan)" "Toner (Black)" "Drum Cartridge (Cyan)" "Developer Cartridge (Cyan)" "Drum Cartridge (Magenta)" "Developer Cartridge (Magenta)" "Drum Cartridge (Yellow)" "Developer Cartridge (Yellow)" "Drum Cartridge (Black)" "Developer Cartridge (Black)" "Fusing Unit" "Image Transfer Belt Unit" "Transfer Roller Unit") +#End Variables------------- + +echo "Start getting cookie" +get_cookie "$@" +echo "Cookie got" + +echo "Start extract from system_counter" +if [[ $1 == "-d" ]]; then + system_counter_data=$(cat system_counter.xml |xq) +else + system_counter_data=$(curl -X GET http://192.168.1.42/wcd/system_counter.xml -H "Cookie: ID=\"$cookie\"" |xq) +fi + +get_values ".MFP.Count.UserCounterInfo.TotalCounterList.TotalCounter" system_counter_TotalCounter_keys TotalCounter + +get_values ".MFP.Count.UserCounterInfo.PaperSheetCounter.FullColorCounterList.FullColorCounter" system_counter_FullColorCounter_keys FullColorCounter + +get_values ".MFP.Count.UserCounterInfo.PaperSheetCounter.BlackCounterList.BlackCounter" system_counter_BlackCounter_keys BlackCounter + +get_values ".MFP.Count.UserCounterInfo.PaperSheetCounter.DoubleColorCounterList.DoubleColorCounter" system_counter_DoubleColorCounter_keys DoubleColorCounter + +get_values ".MFP.Count.UserCounterInfo.CopyCounterList.CopyCounter" system_counter_CopyCounter_keys CopyCounter + +get_values ".MFP.Count.UserCounterInfo.ScanFaxCounterList.ScanFaxCounter" system_counter_ScanFaxCounter_keys ScanFaxCounter + +get_values_DeviceStatus system_counter_DeviceStatus_keys DeviceStatus + +echo "Stop extract from system_counter" +echo +echo "Start extract from system_consumables" +if [[ $1 == "-d" ]]; then + system_consumables_data=$(cat system_consumables.xml |xq) +else + system_consumables_data=$(curl -X GET http://192.168.1.42/wcd/system_counter.xml -H "Cookie: ID=\"$cookie\"") +fi + +get_values_consumables system_consumables_base_keys Consumables + +echo "Stop extract from system_consumables" + +echo "$valueStore" | curl --data-binary @- http://localhost:9091/metrics/job/printer +echo "Success!" +exit 0 \ No newline at end of file From 360d9b3df7feb338e3eaa8fe10549db192a1f9a1 Mon Sep 17 00:00:00 2001 From: ahtlon Date: Sat, 25 Oct 2025 22:16:03 +0200 Subject: [PATCH 2/3] Add command testing --- machines/overwatch/pull_info.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/machines/overwatch/pull_info.sh b/machines/overwatch/pull_info.sh index 37244b3..f1824bc 100644 --- a/machines/overwatch/pull_info.sh +++ b/machines/overwatch/pull_info.sh @@ -1,6 +1,13 @@ #!/usr/bin/env bash set -o pipefail - +for command in "jq" "xq" "grep" "curl" "sed" +do + if ! command -v $command >/dev/null 2>&1 + then + echo "$command could not be found" + exit 1 + fi +done #Functions--------------- get_cookie () { if [[ $1 == "-d" ]]; then From 523005ed9464d8e44e276cff83dab72cd7543b41 Mon Sep 17 00:00:00 2001 From: ahtlon Date: Sat, 25 Oct 2025 22:16:38 +0200 Subject: [PATCH 3/3] Fix string warning, script execution --- machines/overwatch/printer_module.nix | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/machines/overwatch/printer_module.nix b/machines/overwatch/printer_module.nix index 8d457ff..0eda1d1 100644 --- a/machines/overwatch/printer_module.nix +++ b/machines/overwatch/printer_module.nix @@ -4,10 +4,10 @@ enable = lib.mkOption { type = lib.types.bool; default = false; - description = "Enable the script to pull data from the printer" + description = "Enable the script to pull data from the printer"; }; timer = lib.mkOption { - type = lib.types.string; + type = lib.types.str; default = "1m"; description = "systemd timer for script execution"; }; @@ -17,13 +17,13 @@ systemd.services."printer-scraping" = { description = "Pull printer stats and upload to influxdb"; serviceConfig.Type = "oneshot"; - path = with pkgs; [yq curl]; - script = "exec ${./pull_info.sh}"; + path = with pkgs; [yq jq curl bash]; + script = "bash ${./pull_info.sh}"; }; systemd.timers."printer-scraping" = { wantedBy = ["timers.target"]; timerConfig = { - OnBootSec = "60s"; + OnBootSec = "5s"; OnUnitActiveSec = config.printer_scraping.timer; Unit = "printer-scraping.service"; };