PDA

View Full Version : Bug tracking for reading XML file using SimpleXML


Carmageddon
12-01-2009, 04:09 AM
Hey guys!

I've built a custom status page for the place I work at, where we need to monitor a dynamic number of servers (usually around 20).

I am reading each server's status file via another script that runs every minute or so, exporting XML file with its current status.

Then, my php status.php page, would read these XML files, and using regex parsers I wrote, extract the data I need, compare, do whats needed, print output in tables etc, and store the current status in status.xml file.

So far no issues (well I solved them all over time hehe),
The thing is, the status page has a lot of information we store regarding the servers - each server (table line) has its own notes about it, various check boxes for different things we would check and uncheck as needed - all of these, get submitted by single form submit button, and when that is done - everything is processed, written to the loaded SimpleXML Object, then written instead of the old file.


The above process is done, by unlinking the old xml file, then writing the object to new status.xml file like this:
$simplXMLobj->asXML('/var/www/html/Dev/Notes/status.xml');

The file is being read using this line:
$xmlNotes = simplexml_load_file('/var/www/html/Dev/Notes/status.xml');


the XML file structure is like this: (obviously different values, for confidentiality reasons):

<servers>
<server02>
<feature1>1</feature1>
<note>server needs reboot</note>
</server02>
<server03>
<feature1>0</feature1>
<note>server needs food</note>
</server03>
</servers>


I am iterating over all XML files, and since they include the server number, its easy for me to refer to the correct XML element without iterating the whole XML document for every single server (table line) - which is why I use SimpleXML - cant find any easier way.


It works perfectly fine 99.9% of the time (page is set on autorefresh every 60 secs, redirect back as well in case of submitting updates).

Problem arises randomly, giving out errors such as this:

file() [function.simplexml-load-file]: /var/www/html/Dev/Notes/status.xml:15: parser error : StartTag: invalid element name in /var/www/html/status.php on line 33

Warning: simplexml_load_file() [function.simplexml-load-file]: ver35><server35><TESTCASE>-</TESTCASE></server35><server35><Version>-</Version>< in /var/www/html/status.php on line 33

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /var/www/html/status.php on line 33

Warning: simplexml_load_file() [function.simplexml-load-file]: /var/www/html/Dev/Notes/status.xml:15: parser error : Premature end of data in tag server35 line 15 in /var/www/html/status.php on line 33

And few more like that.. ending up on this attempt to write changes to the file:

Fatal error: Call to undefined method stdClass::asXML() in /var/www/html/status.php on line 295



I think I know the problem - on a rare occasion, someone else (as I said between 5-8 computers watch that page at any given time...) invoked the script, and it happen to be either unlinked, or writing the file at that exact moment...

I think so, because next time I manually refresh the page (F5), the error is gone.. everything back to normal.


Any ideas how to debug this? I was thinking of some kind of Try-Catch block for these 2 functions... but how exactly to write that? I would like to write errors to some file somehow, them mailing myself when errors were written.. however I am unsure if this function supports such block.. or how to write it to be honest.


Thanks guys! hope we solve this one, and perhaps I even get ideas how to improve the thing here :)