-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipmi.sh
72 lines (61 loc) · 1.85 KB
/
ipmi.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# Declare an associative array to store sensor data
declare -A sensor_data
# Initialize index for the array
index=1
# Read lines from ipmitool sensor output
while read -r line; do
# Ignore lines starting with "IANA" or "Received"
if [[ "$line" =~ ^IANA|^Received ]]; then
continue
fi
# Extract name, value, and unit from the line
name=$(echo "$line" | cut -d'|' -f1 | tr -d ' ')
value=$(echo "$line" | cut -d'|' -f2 | tr -d ' ')
unit=$(echo "$line" | cut -d'|' -f3 | tr -d ' ')
# Ignore lines with "na" in the value
if [[ "$value" =~ na ]]; then
continue
fi
# Remove ".000" from the value if present
value="${value/\.000/}"
# Store the name:value:unit in the associative array with the index as the key
sensor_data[$index]="$name: $value: $unit"
# Increment the index for the next line
index=$((index + 1))
done < <(ipmitool sensor)
# Construct the XML output without loops
output="<prtg>
"
for i in $(seq 1 "$((index - 1))"); do
sensor_value="${sensor_data[$i]}"
name=$(echo "$sensor_value" | cut -d':' -f1 | tr -d ' ')
value=$(echo "$sensor_value" | cut -d':' -f2 | tr -d ' ')
unit=$(echo "$sensor_value" | cut -d':' -f3 | tr -d ' ')
# Check if the value has a decimal part
if [[ "$value" =~ "." ]]; then
output="$output<result>
<channel>$name</channel>
<value>$value</value>
<float>1</float>
<unit>custom</unit>
<customunit>$unit</customunit>
</result>"
elif [[ "$unit" =~ "degreesC" ]]; then
output="$output<result>
<channel>$name</channel>
<value>$value</value>
<unit>Temperature</unit>
</result>"
else
output="$output<result>
<channel>$name</channel>
<value>$value</value>
<unit>custom</unit>
<customunit>$unit</customunit>
</result>"
fi
done
output="$output</prtg>"
# Echo the entire XML output in one command
echo "$output"