The following example illustrates how to get a monitor status information from AlertSite using a Perl script. This involves logging in to the account (Function 1), listing the monitors (Function 3), getting the monitor status information (Function 8), and logging out (Function 2).
Password information has been changed to XXXXXX for display purposes.
Copy-and-paste of this example may introduce hidden characters and break the program, requiring some editing. Also, it is not necessary to use Perl. Any programming language with HTTP/CGI support may be used. |
Perl
#!/usr/bin/perl
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
# #
# EXAMPLE CODE ONLY - DO NOT COPY-AND-PASTE! #
# #
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
##############################################################################
# REST client - Login, List Monitors, Get monitor status of a specific monitor #
# #
# NOTE: This is a non-browser REST client, so we need to manually store the #
# cookie that the server sends back. #
# #
# First we login with the existing REST API for Login (via URL /user/login), #
# and we will get a cookie back from the Set-Cookie header. We also get #
# back a response XML. #
##############################################################################
use strict;
use HTTP::Request::Common;
use LWP::UserAgent;
my $REST_SERVER = 'https://www.alertsite.com/restapi'; # Base path
my $LOGIN = '[email protected]'; # AlertSite account login
my $PASSWORD = 'mypassword'; # AlertSite account password
#############################
# Set up User Agent #
#############################
my $ua = LWP::UserAgent->new;
$ua->agent('AlertSite REST Client/1.0');
my ($POST_XML, $req, $resp, $cookie, $session, $OBJCUST, $DEVICE);
#############################
# 1. Login request #
#############################
my $POST_XML_LOGIN = << "POST_XML_LOGIN"; # Request body
<Login> <Login>$LOGIN</Login> <Password>$PASSWORD</Password> </Login>
POST_XML_LOGIN
# Set up HTTP request to login.
# Use text/xml and raw POST data to conform to existing REST API
$req = HTTP::Request->new(POST => "$REST_SERVER/user/login");
$req->content_type('text/xml');
$req->content($POST_XML_LOGIN);
print "\n###### LOGIN Request ######\n\n"
.$req->as_string;
print "\n###########################\n";
$resp = $ua->request($req); # Send request
$cookie = $resp->header('Set-Cookie'); # Save cookie
# Save Session ID and Customer Object ID for subsequent API calls
($session) = $resp->content =~ m|<SessionID>(\w+)</SessionID>|;
($OBJCUST) = $resp->content =~ m|<ObjCust>(\w+)</ObjCust>|;
print "\n###### LOGIN Response (Header and Body) ######\n\n"
.$resp->as_string;
print "##############################################\n";
##############################
# 2. List Monitors Request #
##############################
my $POST_XML = << "POST_XML"; # Request body
<List>
<TxnHeader>
<Request>
<Login>_LOGIN_</Login>
<SessionID>_SESSION_</SessionID> </Request>
</TxnHeader>
<Source></Source>
</List>
POST_XML
# Set Login and Session ID from login request response
$POST_XML =~ s/_LOGIN_/$LOGIN/;
$POST_XML =~ s/_SESSION_/$session/;
# Set up HTTP request to list monitors and include the cookie from login.
# Use text/xml and raw POST data to conform to existing REST API.
$req = HTTP::Request->new(POST => "$REST_SERVER/devices/list");
$req->header(Cookie => $cookie);
$req->content_type('text/xml');
$req->content($POST_XML);
print "\n###### LIST MONITORS Request ######\n\n"
.$req->as_string;
print "\n##################################\n";
$resp = $ua->request($req);
print "\n###### LIST MONITORS Response (Header and Body) ######\n\n"
.$resp->as_string;
print "#####################################################\n";
################################
# 3. Get Monitor Status Request #
################################
#======================================================================#
# For the Get Monitor Status request, $DEVICE must be set to a specific #
# Monitor ID. To get the monitor ID of a specific monitor, do a LIST #
# MONITORS call with $DEVICE not set, which returns the status of all #
# monitors. Parse the XML code returned in the output to select the #
# desired ObjDevice number, and set $DEVICE to that value. #
# #
# In this example, $DEVICE set to 70215 after initial run. #
#======================================================================#
$DEVICE = 70215; # Monitor ID from List Monitors run
$POST_XML = << "POST_XML"; # Request body
<Status>
<TxnHeader>
<Request>
<Login>_LOGIN_</Login>
<SessionID>_SESSION_</SessionID>
<ObjCust>_OBJCUST_</ObjCust>
<ObjDevice>_OBJDEVICE_</ObjDevice>
</Request>
</TxnHeader>
<Source></Source>
</Status>
POST_XML
# Set Login and Session ID from login request response
# Set Customer Object ID and monitor ID from list request response
$POST_XML =~ s/_LOGIN_/$LOGIN/;
$POST_XML =~ s/_SESSION_/$session/;
$POST_XML =~ s/_OBJCUST_/$OBJCUST/;
$POST_XML =~ s/_OBJDEVICE_/$DEVICE/;
# Set up HTTP request to list monitors and include the cookie from login.
# Use text/xml and raw POST data to conform to existing REST API.
$req = HTTP::Request->new(POST => "$REST_SERVER/devices/status");
$req->header(Cookie => $cookie);
$req->content_type('text/xml');
$req->content($POST_XML);
print "\n###### GET MONITOR STATUS Request ######\n\n"
.$req->as_string;
print "\n#######################################\n";
$resp = $ua->request($req);
print "\n###### GET MONITOR STATUS Response (Header and Body) ######\n\n"
.$resp->as_string;
print "\n##########################################################\n";
Output From Execution
Running the above program produces the following output.
###### LOGIN Request ######
POST https://www.alertsite.com/restapi/user/login
Content-Type: text/xml
<Login>
<Login>[email protected]</Login>
<Password>mypassword</Password>
</Login>
###########################
###### LOGIN Response (Header and Body) ######
HTTP/1.1 200 OK
Connection: close
Date: Wed, 16 Dec 2009 18:42:47 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Content-Type: text/xml
Client-Date: Wed, 16 Dec 2009 18:43:12 GMT
Client-Peer: 204.2.244.15:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=ZA/ST=Western Cape/L=Cape Town/O=Thawte Consulting cc/OU=Certification Services Division
/CN=Thawte Premium Server CA/[email protected]
Client-SSL-Cert-Subject: /C=US/ST=Florida/L=Boca Raton/O=Boca Internet Technologies, Inc./CN=www.alertsite.com
Client-SSL-Cipher: DHE-RSA-AES256-SHA
Client-SSL-Warning: Peer certificate not verified
Client-Transfer-Encoding: chunked
Set-Cookie: as_sessionid=z6jypy753rxx6v4um7nnlgofxc6revh4jcoicogrlksgy3erlyyz2xutqx7gsqhfuvwukk5izkp6w;
domain=.alertsite.com; path=/; secure
<Response>
<Status>0</Status>
<Message>No errors.</Message>
<SessionID>62077a838640ca3f</SessionID>
<ObjDevice></ObjDevice>
<ObjCust>35531</ObjCust>
<ObjParent>57</ObjParent>
<ObjCust>35531</ObjCust>
<Login>[email protected]</Login>
<Custid>C45531</Custid>
<Company>Widgets R Us</Company>
<CurrPlan></CurrPlan>
<CurrInterval></CurrInterval>
<AvailablePlans>DEJAPRO5:5:10,INSITE-25:1:25</AvailablePlans>
<TxnOut>
</TxnOut>
</Response>
##############################################
###### LIST MONITORS Request ######
POST https://www.alertsite.com/restapi/devices/list
Content-Type: text/xml
Cookie: as_sessionid=z6jypy753rxx6v4um7nnlgofxc6revh4jcoicogrlksgy3erlyyz2xutqx7gsqhfuvwukk5izkp6w;
domain=.alertsite.com; path=/; secure
<List>
<TxnHeader>
<Request>
<Login>[email protected]</Login>
<SessionID>62077a838640ca3f</SessionID>
</Request>
</TxnHeader>
<Source></Source>
</List>
##################################
###### LIST MONITORS Response (Header and Body) ######
HTTP/1.1 200 OK
Connection: close
Date: Wed, 16 Dec 2009 18:42:48 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Content-Type: text/xml
Client-Date: Wed, 16 Dec 2009 18:43:12 GMT
Client-Peer: 204.2.244.15:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=ZA/ST=Western Cape/L=Cape Town/O=Thawte Consulting cc/OU=Certification Services Division
/CN=Thawte Premium Server CA/[email protected]
Client-SSL-Cert-Subject: /C=US/ST=Florida/L=Boca Raton/O=Boca Internet Technologies, Inc./CN=www.alertsite.com
Client-SSL-Cipher: DHE-RSA-AES256-SHA
Client-SSL-Warning: Peer certificate not verified
Client-Transfer-Encoding: chunked
<Response>
<Status>0</Status>
<Message>No errors.</Message>
<SessionID>62077a838640ca3f</SessionID>
<ObjDevice></ObjDevice>
<ObjCust>35531</ObjCust>
<ObjParent>57</ObjParent>
<ObjCust>35531</ObjCust>
<Login>[email protected]</Login>
<Custid>C45531</Custid>
<Company>Widgets R Us</Company>
<CurrPlan></CurrPlan>
<CurrInterval></CurrInterval>
<AvailablePlans>INSITE-25:1:25,WTPRO5:5:20</AvailablePlans>
<TxnList>
<Txn>
<TxnName>InSite%20Test%201%20-%20yahoo</TxnName>
<TxnDetail Monitor="y" Notify="y" PingError="n" TraceError="y" Interval="5" TimeOut="30" ObjDevice="71277"
BillPlan="INSITE-25" MaxSteps="25"/>
</Txn>
<Txn>
<TxnName>InSite%20Test%202%20-%20google</TxnName>
<TxnDetail Monitor="n" Notify="y" PingError="n" TraceError="y" Interval="5" TimeOut="30" ObjDevice="71278"
BillPlan="INSITE-25" MaxSteps="25"/>
</Txn>
<Txn>
<TxnName>InSite%20Test%203%20-%20msnbc</TxnName>
<TxnDetail Monitor="n" Notify="y" PingError="n" TraceError="y" Interval="5" TimeOut="30" ObjDevice="71279"
BillPlan="INSITE-25" MaxSteps="25"/>
</Txn>
<Txn>
<TxnName>InSite%20Test%204%20-%20espn</TxnName>
<TxnDetail Monitor="n" Notify="y" PingError="n" TraceError="y" Interval="5" TimeOut="30" ObjDevice="71280"
BillPlan="INSITE-25" MaxSteps="25"/>
</Txn>
<Txn>
<TxnName>InSite%20Test%205%20-%20cbs</TxnName>
<TxnDetail Monitor="n" Notify="y" PingError="n" TraceError="y" Interval="5" TimeOut="30" ObjDevice="71281"
BillPlan="PPRO5" MaxSteps="0"/>
</Txn>
<Txn>
<TxnName>JB%20-%20Site</TxnName>
<TxnDetail Monitor="y" Notify="y" PingError="n" TraceError="y" Interval="5" TimeOut="45" ObjDevice="70324"
BillPlan="INSITE-25" MaxSteps="25"/>
</Txn>
<Txn>
<TxnName>www.alertsite.com</TxnName>
<TxnDetail Monitor="n" Notify="n" PingError="n" TraceError="y" Interval="15" TimeOut="20" ObjDevice="69919"
BillPlan="SLA5" MaxSteps="0"/>
</Txn>
<Txn>
<TxnName>www.google.com</TxnName>
<TxnDetail Monitor="n" Notify="y" PingError="n" TraceError="y" Interval="15" TimeOut="20" ObjDevice="70215"
BillPlan="PPRO5" MaxSteps="0"/>
</Txn>
</TxnList>
<TxnOut>
</TxnOut>
</Response>
#####################################################
###### GET MONITOR STATUS Request ######
POST https://www.alertsite.com/restapi/devices/status
Content-Type: text/xml
Cookie: as_sessionid=z6jypy753rxx6v4um7nnlgofxc6revh4jcoicogrlksgy3erlyyz2xutqx7gsqhfuvwukk5izkp6w;
domain=.alertsite.com; path=/; secure
<Status>
<TxnHeader>
<Request>
<Login>[email protected]</Login>
<SessionID>62077a838640ca3f</SessionID>
<ObjCust>35531</ObjCust>
<ObjDevice>70215</ObjDevice>
</Request>
</TxnHeader>
<Source>Widgets R Us</Source>
</Status>
#######################################
###### GET MONITOR STATUS Response (Header and Body) ######
HTTP/1.1 200 OK
Connection: close
Date: Wed, 16 Dec 2009 18:42:49 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Content-Type: text/xml
Client-Date: Wed, 16 Dec 2009 18:43:14 GMT
Client-Peer: 204.2.244.15:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=ZA/ST=Western Cape/L=Cape Town/O=Thawte Consulting cc/OU=Certification Services Division
/CN=Thawte Premium Server CA/[email protected]
Client-SSL-Cert-Subject: /C=US/ST=Florida/L=Boca Raton/O=Boca Internet Technologies, Inc./CN=www.alertsite.com
Client-SSL-Cipher: DHE-RSA-AES256-SHA
Client-SSL-Warning: Peer certificate not verified
Client-Transfer-Encoding: chunked
<Response>
<Status>0</Status>
<Message>No errors.</Message>
<SessionID>62077a838640ca3f</SessionID>
<ObjDevice>70215</ObjDevice>
<ObjCust>35531</ObjCust>
<Custid>C45531</Custid>
<Company>Widgets R Us</Company>
<CurrPlan></CurrPlan>
<CurrInterval></CurrInterval>
<AvailablePlans>INSITE-25:1:25,WTPRO5:5:20</AvailablePlans>
<DeviceStatuses>
<Device>
<ObjDevice>70215</ObjDevice>
<Descrip>www.google.com</Descrip>
<ObjLocation>20</ObjLocation>
<Location>Atlanta, Georgia</Location>
<Monitor>n</Monitor>
<LastStatusCode>0</LastStatusCode>
<LastStatus>Site is OK Now!</LastStatus>
<DtLastStatus>2009-12-15 23:03:03</DtLastStatus>
<DtLastError></DtLastError>
<DtLastErrorCleared></DtLastErrorCleared>
</Device>
<Device>
<ObjDevice>70215</ObjDevice>
<Descrip>www.google.com</Descrip>
<ObjLocation>72</ObjLocation>
<Location>Boston, Massachusetts</Location>
<Monitor>n</Monitor>
<LastStatusCode>0</LastStatusCode>
<LastStatus>Site is OK Now!</LastStatus>
<DtLastStatus>2009-12-15 22:48:04</DtLastStatus>
<DtLastError></DtLastError>
<DtLastErrorCleared></DtLastErrorCleared>
</Device>
<Device>
<ObjDevice>70215</ObjDevice>
<Descrip>www.google.com</Descrip>
<ObjLocation>52</ObjLocation>
<Location>Chicago, Illinois - Ubiquity</Location>
<Monitor>n</Monitor>
<LastStatusCode>0</LastStatusCode>
<LastStatus>Site is OK Now!</LastStatus>
<DtLastStatus>2009-12-15 23:03:03</DtLastStatus>
<DtLastError></DtLastError>
<DtLastErrorCleared></DtLastErrorCleared>
</Device>
</DeviceStatuses>
</Response>
##########################################################