Every couple of years I manage a field full of Cisco VG224 Voice Gateways, collocated in portable toilets. They're connected to a patch panel via a chunky, 25 pair "RJ21" cable which occasionally become dislodged, and I'm unaware until someone reports that their phone has stopped working, which isn't ideal.
The Cisco hardware provides some line testing facilities, so I wondered if I could utilise them to check the status of the cable remotely, and hopefully push the status to a monitoring platform such as Prometheus via push-gateway.
Note, running the tests requires the voice-port to be shutdown, and something connected to the line. I opted for a simple 470 ohm resistor between the A/B (Tip/Ring) conductors.
Running a loop-resistance looks something like this:
vg224# test voice port 0/0/3 line-test loop-resistance
Loop resistance measurement between tip-ring: 447 Ohm
So now we need a script to run the command, parse the output, and POST the result somewhere. My initial attempts failed, with the script throwing errors about the HTTP library. Fortunately with a hint from Darren Parkinson's Blog and a bit of "Vibe Coding" I managed to get it working,
First off we need to create the post_loop_resistance.tcl script. This is formatted so you can paste it directly into the Cisco console (Without being in the config terminal). Remember to update the LOOP_PORT and URL to suit your environment:
tclsh
set fh [open "flash:/post_loop_resistance.tcl" w]
puts $fh {
# Measures loop resistance and POSTs result to HTTP endpoint
# ---- CONFIG ----
set LOOP_PORT "0/0/3"
set URL "http://webhook.site/foo"
# ----------------
source "tmpsys:lib/tcl/http.tcl"
# Ensure privileged mode (safe if already enabled)
catch {exec "enable"}
# Run the line test
set output [exec "test voice port $LOOP_PORT line-test loop-resistance"]
# Normalise output
regsub -all {\r} $output {} output
# Extract numeric value
set resistance ""
if {[regexp -nocase {loop resistance.*:\s*([0-9]+)\s*(ohm|ohms)} $output -> resistance unit]} {
# ok
} elseif {[regexp {:\s*([0-9]+)} $output -> resistance]} {
# fallback
} else {
puts "ERROR: could not parse resistance"
puts "RAW:$output"
exit 1
}
# Build Prometheus text body
set body [format "loop_resistance_ohms{port=\"%s\"} %s\n" $LOOP_PORT $resistance]
# POST the result
set Token [::http::geturl $URL -type "text/plain" -query $body]
# Print the response
puts [::http::data $Token]
# Cleanup
::http::cleanup $Token}
close $fh
exit
To test its working you can manually run the tcl script, and you should see the HTTP POST hit your webserver:
vg224# tclsh flash:/post_loop_resistance.tcl
conf t
event manager applet POST_LOOP_METRIC
event timer watchdog time 300
action 1.0 cli command "enable"
action 2.0 cli command "tclsh flash:/post_loop_resistance.tcl"
end
You should now see the loop-resistance being POST'ed every 5 minutes.
The script is quite clean and easy to read so you should be able to customise it to POST any metric you want.
No comments:
Post a Comment