-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnQuery.pl
More file actions
executable file
·128 lines (104 loc) · 3.99 KB
/
snQuery.pl
File metadata and controls
executable file
·128 lines (104 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/perl
# -------------------------------------------
# snQuery.pl
# Tim Pilson
# 3/18/2014
#
# Manually query servers from ServiceNow CMDB
# -------------------------------------------
use strict;
use SOAP::Lite;
use Term::ReadKey;
## Required config file
my($instance,$sn_user);
open(my $config, "<", "config.txt") || die "ERROR: can't open config file\n";
while (<$config>) {
chomp;
next if ( /^\#/ );
my($key,$value) = split(/:/);
if ( $key eq "instance" ) { $instance = $value }
if ( $key eq "sn_user" ) { $sn_user = $value }
}
## Check config parameters
if (( $sn_user eq "" ) || ( $instance eq "" )) {
print "ERROR: missing config parameters\n";
exit 1;
}
## Get Credentials for Authentication
print "Enter Password: ";
ReadMode('noecho'); ## turn off echo for grabbing password
chomp(my $password = <STDIN>);
ReadMode(0);
print "\n";
## SOAP basic auth
sub SOAP::Transport::HTTP::Client::get_basic_credentials {
return "$sn_user" => "$password";
}
## SOAP specify the endpoint to connect
my $soap = SOAP::Lite
->proxy("https://$instance/cmdb_ci_server.do?SOAP");
## Get total number of records first
## Since ServiceNow's web services will only return a
## a total of 250 records, this "windowing" method will
## batch the queries to get the full result set.
## ------------------------------------------------------
my $method = SOAP::Data->name('getKeys')
->attr({xmlns => 'http://www.service-now.com/'});
## Query parameter
push(my @params, SOAP::Data->name("operational_status" => "1") );
## Get results, divide into array
my $result = $soap->call($method => @params)->result;
my @serverKeys = split(",", $result);
my $totalCount = @serverKeys;
## Re-establish connection, use windowing to get all records
my $windowSize = 100; ## Max 250
my $lastRow = 0; ## initialize
my $count = 0; ## initialize
## Loop through results, offset query start row to get windowing
for ( my $i = 0; ($lastRow +1) < $totalCount; $i += ($windowSize) ) {
$lastRow = ($i + $windowSize);
print "Querying rows: $i \- $lastRow\n\n";
my $soap = SOAP::Lite
-> proxy('https://southwestdev.service-now.com/cmdb_ci_server.do?displayvalue=true&SOAP');
my $method = SOAP::Data->name("getRecords")->
attr({xmlns => "http://www.service-now.com/"});
my @params = (SOAP::Data->name("operational_status" => "1"));
## Starting offset of records (windowing)
push(@params, SOAP::Data->name(__first_row => "$i") );
## Determine last row
## Query fails if __last_row is greater than total records
## __last_row is not inclusive of the last row.
if ( $lastRow > $totalCount ) {
push(@params, SOAP::Data->name(__last_row => "$totalCount") );
} else {
push(@params, SOAP::Data->name(__last_row => "$lastRow") );
}
my $result = $soap->call($method => @params);
print_fault($result);
my @serverData = @{$result->body->{getRecordsResponse}->{getRecordsResult}};
## Loop through results
## Use this section for determining which results
## are needed.
## ------------------------------------------------
foreach my $serverRec (@serverData) {
print "$serverRec->{name}\n";
print " model_id: $serverRec->{model_id}\n";
print " serial_number: $serverRec->{serial_number}\n";
print " os: $serverRec->{os}\n";
print " os_version: $serverRec->{os_version}\n";
print " os_service_pack: $serverRec->{os_service_pack}\n";
print " short_description: $serverRec->{short_description}\n";
print " hardware_status: $serverRec->{hardware_status}\n";
print " first_discovered: $serverRec->{first_discovered}\n";
print " sys_class_name: $serverRec->{sys_class_name}\n";
print " used_for: $serverRec->{used_for}\n\n";
}
}
sub print_fault {
my ($result) = @_;
if ($result->fault) {
print "faultcode: " . $result->fault->{'faultcode'} . "\n";
print "faultstring: " . $result->fault->{'faultstring'} . "\n";
print "detail: " . $result->fault->{'detail'} . "\n";
}
}