Monitor API With Nagios xi

This support forum board is for support questions relating to Nagios xi, our flagship commercial network monitoring solution.
ABBsupervision
Posts: 41
Joined: Tue Jun 08, 2021 4:20 am

Monitor API With Nagios xi

Post by ABBsupervision »

Hello team,

I want to monitor API [ http://IPADRESS:PORT/SoapHttpPort?WSDL ] via Nagios xi

the url contains parameters as you can see below :

Code: Select all

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hps="http://hps.ws.service/" xmlns:typ="http://hps.ws.service/types/">
   <soapenv:Header/>
   <soapenv:Body>
      <hps:hpsService>
         <login>nagios</login>                                      <!-- exemple -->
         <passwd>nagiosxi</passwd>                             <!-- exemple -->
         <functionName>card_details</functionName>  <!-- exemple -->
         <args>
            
            <typ:String>token</typ:String>                      <!-- Token -->
            <typ:String>c_client</typ:String>                   <!-- Code client -->
         </args>
      </hps:hpsService>
   </soapenv:Body>
</soapenv:Envelope>
And the result I want to monitor is the code returned by the API:

Code: Select all

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns3:hpsServiceResponse xmlns:ns2="http://hps.ws.service/types/" xmlns:ns3="http://hps.ws.service/">
         <result><![CDATA[<?xml version='1.0' encoding='UTF-8'?><HPSRESULT><RETURN_STATUS>0</RETURN_STATUS><p_return_code>00000</p_return_code><p_error_description>OPERATION REUSSIE</p_error_description><p_new_card_number>4******************</p_new_card_number><p_expiry_date>30/04/23</p_expiry_date><p_CVV2>null</p_CVV2><p_new_card_trunc>48****************6</p_new_card_trunc><p_network>VISA</p_network></HPSRESULT>]]></result>
      </ns3:hpsServiceResponse>
   </S:Body>
</S:Envelope>

if the returned code is 0000 Nagios must display that the api is OK, otherwise nagios must report that the api is in critical status


Any idea PLEASE ?
SOUHAIB
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: Monitor API With Nagios xi

Post by gsmith »

HI

The check_http plugin should be able to accomplish this for you.

Here is it's description/help:

Code: Select all

[root@gs-cent8-23-82 libexec]# ./check_http -h
check_http v2.3.3 (nagios-plugins 2.3.3)
Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
Copyright (c) 1999-2014 Nagios Plugin Development Team
        <devel@nagios-plugins.org>

This plugin tests the HTTP service on the specified host. It can test
normal (http) and secure (https) servers, follow redirects, search for
strings and regular expressions, check connection times, and report on
certificate expiration times.


Usage:
 check_http -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]
       [-J <client certificate file>] [-K <private key>]
       [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L] [-E] [-U] [-a auth]
       [-b proxy_auth] [-f <ok|warning|critical|follow|sticky|stickyport>]
       [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]
       [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]
       [-A string] [-k string] [-S <version>] [--sni] [--verify-host]
       [-C <warn_age>[,<crit_age>]] [-T <content-type>] [-j method]
NOTE: One or both of -H and -I must be specified

Options:
 -h, --help
    Print detailed help screen
 -V, --version
    Print version information
 --extra-opts=[section][@file]
    Read options from an ini file. See
    https://www.nagios-plugins.org/doc/extra-opts.html
    for usage and examples.
 -H, --hostname=ADDRESS
    Host name argument for servers using host headers (virtual host)
    Append a port to include it in the header (eg: example.com:5000)
 -I, --IP-address=ADDRESS
    IP address or name (use numeric address if possible to bypass DNS lookup).
 -p, --port=INTEGER
    Port number (default: 80)
 -4, --use-ipv4
    Use IPv4 connection
 -6, --use-ipv6
    Use IPv6 connection
 -S, --ssl=VERSION[+]
    Connect via SSL. Port defaults to 443. VERSION is optional, and prevents
    auto-negotiation (2 = SSLv2, 3 = SSLv3, 1 = TLSv1, 1.1 = TLSv1.1,
    1.2 = TLSv1.2). With a '+' suffix, newer versions are also accepted.
 --sni
    Enable SSL/TLS hostname extension support (SNI)
 --verify-host
    Verify SSL certificate is for the -H hostname (with --sni and -S)
 -C, --certificate=INTEGER[,INTEGER]
    Minimum number of days a certificate has to be valid. Port defaults to 443
    (When this option is used the URL is not checked by default. You can use
     --continue-after-certificate to override this behavior)
 --continue-after-certificate
    Allows the HTTP check to continue after performing the certificate check.
    Does nothing unless -C is used.
 -J, --client-cert=FILE
   Name of file that contains the client certificate (PEM format)
   to be used in establishing the SSL session
 -K, --private-key=FILE
   Name of file containing the private key (PEM format)
   matching the client certificate
 -e, --expect=STRING
    Comma-delimited list of strings, at least one of them is expected in
    the first (status) line of the server response (default: HTTP/1.)
    If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)
 -d, --header-string=STRING
    String to expect in the response headers
 -s, --string=STRING
    String to expect in the content
 -u, --uri=PATH
    URI to GET or POST (default: /)
 --url=PATH
    (deprecated) URL to GET or POST (default: /)
 -P, --post=STRING
    URL encoded http POST data
 -j, --method=STRING  (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, CONNECT)
    Set HTTP method.
 -N, --no-body
    Don't wait for document body: stop reading after headers.
    (Note that this still does an HTTP GET or POST, not a HEAD.)
 -M, --max-age=SECONDS
    Warn if document is more than SECONDS old. the number can also be of
    the form "10m" for minutes, "10h" for hours, or "10d" for days.
 -T, --content-type=STRING
    specify Content-Type header media type when POSTing

 -l, --linespan
    Allow regex to span newlines (must precede -r or -R)
 -r, --regex, --ereg=STRING
    Search page for regex STRING
 -R, --eregi=STRING
    Search page for case-insensitive regex STRING
 --invert-regex
    Return CRITICAL if found, OK if not

 -a, --authorization=AUTH_PAIR
    Username:password on sites with basic authentication
 -b, --proxy-authorization=AUTH_PAIR
    Username:password on proxy-servers with basic authentication
 -A, --useragent=STRING
    String to be sent in http header as "User Agent"
 -k, --header=STRING
    Any other tags to be sent in http header. Use multiple times for additional headers
 -E, --extended-perfdata
    Print additional performance data
 -U, --show-url
    Print URL in msg output in plain text
 -L, --link
    Wrap output in HTML link (obsoleted by urlize)
 -f, --onredirect=<ok|warning|critical|follow|sticky|stickyport>
    How to handle redirected pages. sticky is like follow but stick to the
    specified IP address. stickyport also ensures port stays the same.
 -m, --pagesize=INTEGER<:INTEGER>
    Minimum page size required (bytes) : Maximum page size required (bytes)
 -w, --warning=DOUBLE
    Response time to result in warning status (seconds)
 -c, --critical=DOUBLE
    Response time to result in critical status (seconds)
 -t, --timeout=INTEGER:<timeout state>
    Seconds before connection times out (default: 10)
    Optional ":<timeout state>" can be a state integer (0,1,2,3) or a state STRING
 -v, --verbose
    Show details for command-line debugging (Nagios may truncate output)

Notes:
 This plugin will attempt to open an HTTP connection with the host.
 Successful connects return STATE_OK, refusals and timeouts return STATE_CRITICAL
 other errors return STATE_UNKNOWN.  Successful connects, but incorrect response
 messages from the host result in STATE_WARNING return values.  If you are
 checking a virtual server that uses 'host headers' you must supply the FQDN
 (fully qualified domain name) as the [host_name] argument.
 You may also need to give a FQDN or IP address using -I (or --IP-Address).

 This plugin can also check whether an SSL enabled web server is able to
 serve content (optionally within a specified time) or whether the X509
 certificate is still valid for the specified number of days.

 Please note that this plugin does not check if the presented server
 certificate matches the hostname of the server, or if the certificate
 has a valid chain of trust to one of the locally installed CAs.

Examples:
 CHECK CONTENT: check_http -w 5 -c 10 --ssl -H www.verisign.com

 When the 'www.verisign.com' server returns its content within 5 seconds,
 a STATE_OK will be returned. When the server returns its content but exceeds
 the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,
 a STATE_CRITICAL will be returned.

 CHECK CERTIFICATE: check_http -H www.verisign.com -C 14

 When the certificate of 'www.verisign.com' is valid for more than 14 days,
 a STATE_OK is returned. When the certificate is still valid, but for less than
 14 days, a STATE_WARNING is returned. A STATE_CRITICAL will be returned when
 the certificate is expired.


 CHECK CERTIFICATE: check_http -H www.verisign.com -C 30,14

 When the certificate of 'www.verisign.com' is valid for more than 30 days,
 a STATE_OK is returned. When the certificate is still valid, but for less than
 30 days, but more than 14 days, a STATE_WARNING is returned.
 A STATE_CRITICAL will be returned when certificate expires in less than 14 days
 CHECK SSL WEBSERVER CONTENT VIA PROXY USING HTTP 1.1 CONNECT:

 check_http -I 192.168.100.35 -p 80 -u https://www.verisign.com/ -S -j CONNECT -H www.verisign.com
 all these options are needed: -I <proxy> -p <proxy-port> -u <check-url> -S(sl) -j CONNECT -H <webserver>
 a STATE_OK will be returned. When the server returns its content but exceeds
 the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,
 a STATE_CRITICAL will be returned.

Send email to help@nagios-plugins.org if you have questions regarding use
of this software. To submit patches or suggest improvements, send email to
devel@nagios-plugins.org

[root@gs-cent8-23-82 libexec]#

You can supply authorization values and then look for your 000 return code using the -r option.


Please let us know if this doesn't solve your issue.

Thanks!
ABBsupervision
Posts: 41
Joined: Tue Jun 08, 2021 4:20 am

Re: Monitor API With Nagios xi

Post by ABBsupervision »

Hello,

this method monitors the http link if it returns a code however I want to monitor an API with parameters as I mentioned above


Best Regards,
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: Monitor API With Nagios xi

Post by gsmith »

Hi

I believe that plugin will work.

Can you run the curl command against the api and show me the command and response?
(You can redact any sensitive information)

It should be something like:

Code: Select all

curl -v --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction: ACTION_YOU_WANT_TO_CALL" --data @FILE_NAME URL_OF_THE_SOAP_WEB_SERVICE_ENDPOINT
replace the following with actual values:
ACTION_YOU_WANT_TO_CALL
FILE_NAME
URL_OF_THE_SOAP_WEB_SERVICE_ENDPOINT

Thanks
ABBsupervision
Posts: 41
Joined: Tue Jun 08, 2021 4:20 am

Re: Monitor API With Nagios xi

Post by ABBsupervision »

Hello,

Can i ask you what do you mean " FILE_NAME "

Code: Select all

[root@DCPNAGIOSxiN1 ~]# curl -v --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction: G_GET_CARD_DETAILS_TK" --data @FILE_NAME http://IPADRESS:8080/HPSGWS/HPSGWSSoapHttpPort?WSDL
Can you please help me ?
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: Monitor API With Nagios xi

Post by gsmith »

Hi

The file would be an XML file containing:

Code: Select all

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hps="http://hps.ws.service/" xmlns:typ="http://hps.ws.service/types/">
   <soapenv:Header/>
   <soapenv:Body>
      <hps:hpsService>
         <login>nagios</login>                                      <!-- exemple -->
         <passwd>nagiosxi</passwd>                             <!-- exemple -->
         <functionName>card_details</functionName>  <!-- exemple -->
         <args>
           
            <typ:String>token</typ:String>                      <!-- Token -->
            <typ:String>c_client</typ:String>                   <!-- Code client -->
         </args>
      </hps:hpsService>
   </soapenv:Body>
</soapenv:Envelope>

Let me know how it goes.
Thanks
ABBsupervision
Posts: 41
Joined: Tue Jun 08, 2021 4:20 am

Re: Monitor API With Nagios xi

Post by ABBsupervision »

Hello,

The output of the command :

Code: Select all

[root@DCPNAGIOSxiN1 ~]# curl -v --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction: G_GET_CARD_DETAILS_TK" --data @FILE_NAME http://IPADRESS:8080/HPSGWS/HPSGWSSoapHttpPort?WSDL
is :

Code: Select all

[root@DCPNAGIOSxiN1 ~]# curl -v --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction: G_GET_CARD_DETAILS_TK" --data /root/API_HPS http://172.x.x.x:8080/HPSGWS/HPSGWSSoapHttpPort?WSDL
*   Trying 172.x.x.x...
* TCP_NODELAY set
* Connected to 172.x.x.x (IPADRESS) port 8080 (#0)
> POST /HPSGWS/HPSGWSSoapHttpPort?WSDL HTTP/1.1
> Host: 172.x.x.x:8080
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Type: text/xml;charset=UTF-8
> SOAPAction: G_GET_CARD_DETAILS_TK
> Content-Length: 13
>
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 500 Internal Server Error
< Server: Apache-Coyote/1.1
< Content-Type: text/xml;charset=utf-8
< Transfer-Encoding: chunked
< Date: Fri, 11 Mar 2022 09:43:47 GMT
< Connection: close
<
<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"><faultcode>S:Client</faultcode><faultstring>Couldn't create SOAP message due to exception: XML reader error: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '/' (code 47) in prolog; expected '<'
* Closing connection 0
 at [row,col {unknown-source}]: [1,1]</faultstring></S:Fault></S:Body></S:Envelope>[root@DCPNAGIOSxiN1 ~]#
Best Regards,
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: Monitor API With Nagios xi

Post by gsmith »

Hi

It looks like there may be a typo in your /root/API_HPS file.

Could you please post it?

Thanks
ABBsupervision
Posts: 41
Joined: Tue Jun 08, 2021 4:20 am

Re: Monitor API With Nagios xi

Post by ABBsupervision »

Hello,

please find below our file

Code: Select all

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hps="http://hps.ws.service/" xmlns:typ="http://hps.ws.service/types/">
   <soapenv:Header/>
   <soapenv:Body>
      <hps:hpsService>
         <login>nagiosxi</login>
         <passwd>nagiosxi</passwd>
         <functionName>G_GET_CARD_DETAILS_TK</functionName>
         <args>
            
            <typ:String>4846*******</typ:String>              <!-- Token -->
            <typ:String>350810000*****</typ:String>      <!-- Code client -->
         </args>
      </hps:hpsService>
   </soapenv:Body>
</soapenv:Envelope>
Best regards,
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: Monitor API With Nagios xi

Post by gsmith »

Hi

I am not sure why the curl call isn't working...I'm not real familiar with SOAP APIs. I am thinking you
might need to add the option "-X POST" to the curl command.

I did however find this post which should be very helpful for you:
https://stackoverflow.com/questions/233 ... e-response

Thanks