Forum Discussion

Keimond's avatar
Keimond
Icon for Neophyte rankNeophyte
4 years ago

xml and xpaths

Hey all I'm having troubles figuring this out.. so I have set up a datasource that pulls the xml directly from a bind statistics web server and that's working but I can't get the values to show up in the datapoints.

given this small snippet...

<statistics>
  <server>
   <boot-time>2021-05-20T18:36:19.674Z</boot-time>
   <config-time>2021-05-20T18:36:19.842Z</config-time>
   <current-time>2021-05-20T21:43:29.263Z</current-time>
   <counters type="qtype">
      <counter name="A">448606</counter>
      <counter name="PTR">395691</counter>
   </counters>
  </server>
</statistics>

 

When I try "/statistics/server/counters/counter/@name='A'", I just get a value of True back.

When I try  "/statistics/server/counters/counter/@name='A'/text()", I get NAN.

Any ideas on what I can try ? I've been looking at xpath examples all day and haven't been able to figure out what's going on...

 

Additionally if anyone is interested I'll share the datasource after I'm done making it.. I was able to make a complex datasource for the boot/config times with groovy doing something like this:
 

stats = new XmlSlurper().parseText(body);

rawDate = stats.'**'.find { it.name() == 'boot-time' }.text();
Date fd = Date.parse( "yyyy-MM-dd'T'HH:mm:ss.SSS", rawDate );
today = new Date();
timeDiff = (today.time - fd.time)/1000;

return (timeDiff);


Then of course if we want we can set up a threshold to let us know if a dns server was reloaded (config-time) or rebooted (boot-time)

  • Thanks again, I just wanted to show my final output as I added the boot-time and config-time that I wanted as well.
     

    /etc/snmp/scripts/runstats.sh
    
    boottime:79395 conftime:79395 ina:3098500 ina6:8 inaaaa:1107344 inany:69885 incname:43835 inmx:147801 innaptr:23 inns:1 inptr:2260082 insoa:65773 inspf:2244 insrv:97583 intxt:146317 rsnx:16075 outa:1605153 outa6: outaaaa:385541 outany: outcname:18206 outmx:212 outnaptr: outns:75 outptr:123973 outsoa:209 outspf: outsrv:1227 outtxt:288 rsnx:16075 rsfail:477 rserr:455 rsipv4qs:1875597 rsipv4rr:1866673 rsmismatch:1 rsqr:349108 rsqt:9010 rsrtt10:707770 rsrtt100500:76413 rsrtt10100:1082061 rsrtt1600:6 rsrtt500800:300 rsrtt8001600:15 sockopen:1876127 sockclosed:1876115 sockbf:169 consest:1863133 recverr:108


    My modified /etc/snmp/script/runstats.sh is

    #!/bin/sh
    
    rm -rf /var/named/named.stats
    rndc stats
    stats=$(cat /var/named/named.stats | /etc/snmp/scripts/dnsstats.pl 2>/dev/null)
    now=$(date +%s)
    
    host=localhost
    port=8653
    xmlstats=$(curl -s http://${host}:${port} 2>/dev/null | xml2)
    boottime=$(date -d $(echo "${xmlstats}" | egrep -i 'boot-time' | cut -d= -f2) +%s)
    conftime=$(date -d $(echo "${xmlstats}" | egrep -i 'config-time' | cut -d= -f2) +%s)
    boottimeseconds=$(echo "${now} - ${boottime}" | bc)
    conftimeseconds=$(echo "${now} - ${conftime}" | bc)
    
    echo "boottime:${boottimeseconds} conftime:${conftimeseconds} ${stats}"


    Note to anyone out there that also might see this, adding boot-time and config-time does require this in your named.conf
     

    statistics-channels {
            inet 127.0.0.1 port 8653 allow { 127.0.0.1; };
    };

     

  • Anonymous's avatar
    Anonymous

    Yeah, I have a whole slew of datasources i need to push into the exchange. Just gotta find the time. That would have made this easier for you.

  • Ah great thank you I don't think we've ever run across this one when searching for past tools!

  • Anonymous's avatar
    Anonymous

    If you're trying to get the bind stats, give this a shot before building it all again. There's an SNMP version which is older, then there's the SSH version which is newer and easier to implement.

  • And of course after I post this I decide to try one more thing and it works..

    /statistics/server/counters[@type="qtype"]//counter[1]/text()


    I understand why this is working but what if I don't always know if for example the "name=A" is going to be the first element ? Which led me to try
     

    /statistics/server/counters[@type="qtype"]//counter[@name="A"]/text()


    Which does seem to work, so back to building all of the datapoints !