All checks were successful
Check flake syntax / flake-check (push) Successful in 4m42s
134 lines
5.0 KiB
Bash
134 lines
5.0 KiB
Bash
#!/usr/bin/env bash
|
|
set -eo 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
|
|
cookie=$(cat request_example_1.txt)
|
|
else
|
|
cookie=$(curl -s -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 "Getting cookie"
|
|
get_cookie "$@"
|
|
|
|
echo "Start extracting info from system_counter"
|
|
if [[ $1 == "-d" ]]; then
|
|
system_counter_data=$(cat system_counter.xml |xq)
|
|
else
|
|
system_counter_data=$(curl -s -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 "Start extracting info from system_consumables"
|
|
if [[ $1 == "-d" ]]; then
|
|
system_consumables_data=$(cat system_consumables.xml |xq)
|
|
else
|
|
system_consumables_data=$(curl -s -X GET http://192.168.1.42/wcd/system_consumable.xml -H "Cookie: ID=$cookie" |xq)
|
|
fi
|
|
|
|
get_values_consumables system_consumables_base_keys Consumables
|
|
|
|
echo "Sending data to prometheus-pushgateway..."
|
|
|
|
echo "$valueStore" | curl -s --data-binary @- http://localhost:9091/metrics/job/printer
|
|
echo "Success!"
|
|
exit 0
|