3s-gtech Posted August 3, 2022 Posted August 3, 2022 I had a thought yesterday. It was my only one of the day, and it was exhausting. However, it piqued my interest. I'm running APCUPSD on a web server used solely for monitoring the UPS (and others). This provides me with a very simple live web page of the condition of the attached UPS. What's interesting is that the attached UPS has an inlet temperature sensor, which appears to be working accurately. What I'd like to do, is read this temperature figure from the output webpage, as a string, within a page on a completely separate server on our intranet. I understand that I can do this with file_get_contents() and then preg_match() to first load in the page, then examine it searching for a match based on some regex. I can already iframe one page into the other so they're both working okay. What I'm struggling with is getting it to grab the figure I need, which looks like: ITEMP : 21.3 C I want to pull the variable temperature figure into PHP. Any ideas? I can see some good examples online, but struggling to make them work in this context.
ThomL Posted August 3, 2022 Posted August 3, 2022 Can you look at the source page for how it is loading the data and use the same approach rather than trying to somewhat scrape the data from the source page once it's loaded the data?
3s-gtech Posted August 3, 2022 Author Posted August 3, 2022 No, the page is compiled in a CGI application. It won't be somewhat scraping - it'll be very much scraping. I'm fine with that.
ThomL Posted August 3, 2022 Posted August 3, 2022 (edited) Looks like this is what collects/presents/harvests/(...whatever) the data: (https://sourceforge.net/p/apcupsd/svn/HEAD/tree/trunk/examples/gui/status.tcl) [b]set[/b] fd [b][[/b]open "| ./client localhost:7000 status" "r"[b]][/b] [b]while[/b] [b]{[[/b]gets $fd line[b]][/b] > 0[b]}[/b] [b]{[/b] scan $line "%9s" label [b]switch[/b] -- $label [b]{[/b] BCHARGE [b]{[/b] scan $line "%*s : %f Percent" BattChg [b]}[/b] TIMELEFT [b]{[/b] scan $line "%*s : %f Minutes" TimeLeft [b]}[/b] LOADPCT [b]{[/b] scan $line "%*s : %f Percent" LoadPct [b]}[/b] HOSTNAME [b]{[/b] scan $line "%*s : %s" HostName [b]}[/b] RELEASE [b]{[/b] scan $line "%*s : %s" Release [b]}[/b] LINEV [b]{[/b] scan $line "%*s : %f Volts" LineV [b]}[/b] LOTRANS [b]{[/b] scan $line "%*s : %f Volts" LowTrans [b]}[/b] HITRANS [b]{[/b] scan $line "%*s : %f Volts" HiTrans [b]}[/b] RETPCT [b]{[/b] scan $line "%*s : %f Percent" RetPct [b]}[/b] ITEMP [b]{[/b] scan $line "%*s : %f C Internal" ITemp [b]}[/b] LINEFREQ [b]{[/b] scan $line "%*s : %f Hz" LineFreq [b]}[/b] BATTV [b]{[/b] scan $line "%*s : %f Volts" BattV [b]}[/b] NOMBATTV [b]{[/b] scan $line "%*s : %f" NomBattV [b]}[/b] MODEL [b]{[/b] scan $line "%*s : %s" Model [b]}[/b] STATUS [b]{[/b] scan $line "%*s : %s" Status [b]}[/b] I dunno... feels like this could be reverse engineered and used in a more direct way. Can you throw up the part of the source you are looking to scrape, think you might be able to use something like: preg_split('/(ITEMP :)/', $input_line); Edited August 3, 2022 by ThomL 1
3s-gtech Posted August 3, 2022 Author Posted August 3, 2022 Using that I can get ITEMP : to appear in the page output, which is progress - it's capturing that part of the page to the string. What I need is the number (including decimal, so /d is a bit trickier) after it as the string value.
3s-gtech Posted August 3, 2022 Author Posted August 3, 2022 Got it! preg_match('/ITEMP : ((\S+))/', $content, $match); That read the page in $content and outputs the temperature figure as $match[1] I now have a nice neat number in my PHP. Thanks for your help, you pointed me in the right direction! 1
ThomL Posted August 3, 2022 Posted August 3, 2022 Just a case of sorting the regex as you know. Having the 'ITEMP : ' string is most of the way there, can't be much more tweaking needed - if you can post the html line of the source webpage (via view source after page load) I can try to help with the regex further? Although I think you may have this sorted by the time I write this reply.
ThomL Posted August 3, 2022 Posted August 3, 2022 (edited) Seems I was right, thought you were about to sort it! Had this on my clipboard ready to post too (worked testing just the text line without html tags) : preg_split('/(ITEMP : )/', $input_line, $matches); $temp = $matches[1]; Edited August 3, 2022 by ThomL
3s-gtech Posted August 4, 2022 Author Posted August 4, 2022 Doesn't the UPS do SNMP? Probably? It doesn’t have a network port, I’d probably need to buy a card (not sure the ones we have will fit).
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now