-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist-hostwithattr
executable file
·191 lines (154 loc) · 4.35 KB
/
list-hostwithattr
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to search for hosts with certain attributes
#
use strict;
use HOSTDB;
use Getopt::Std;
use vars qw ($opt_h $opt_d $opt_r $opt_v);
getopts ('hdrv');
my $debug = defined ($opt_d);
my $raw = defined ($opt_r);
my $verbose = defined ($opt_v);
if ($#ARGV != 4 || $opt_h) {
die (<<EOH);
Syntax: $0 [-dvr] attribute section type condition match
Options :
-d debug
-r raw output, awkable
-v verbose output, no-op if raw output is selected
attribute is the name
section is the section
type is string, integer or blob
condition is 'eq', 'ne', 'gt' or 'lt'
match is the expected value
EOH
}
my $attribute = shift;
my $section = shift;
my $type = shift;
my $condition = shift;
my $match = shift;
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my @host_refs = find_hostwithattr($hostdb, $attribute, $section, $type, $condition, $match);
if (@host_refs) {
foreach my $host (@host_refs) {
print ("Host :\n\n") unless ($raw);
# interpolation
my $id = $host->id ();
my $ip = $host->ip ();
my $mac = $host->mac_address () || "n/a";
my $hostname = $host->hostname () || 'NULL';
if (! $raw) {
print (<<EOH);
ID $id
---
IP address $ip
Hostname $hostname
MAC address $mac
EOH
}
my @attrs = $host->init_attributes ();
my $numattrs = scalar @attrs;
if (! $raw) {
if ($numattrs > 0) {
print (<<EOH);
Host has $numattrs attributes :
EOH
my $lastsection = '';
foreach my $attr (@attrs) {
my $key = $attr->key ();
my $section = $attr->section ();
my $value = $attr->get ();
if ($section ne $lastsection) {
print ("\n [$section]\n");
}
printf (" %-17s = %s\n", $key, $value);
if ($verbose) {
my $lastupdated = $attr->lastupdated () || 'no lastupdated timestamp';
my $lastmodified = $attr->lastmodified () || 'no lastmodified timestamp';
printf (" %-17s Last modified : %s\n", "", $lastmodified);
printf (" %-17s Last updated : %s\n", "", $lastupdated);
}
}
print ("\n");
}
} else {
# get attribute and print it
foreach my $attr (@attrs) {
printf ("$id $ip $hostname $mac %s %s %s\n", $attr->key (), $attr->section (), $attr->get ());
}
}
print ("---\n\n") unless ($raw);
}
}
sub find_hostwithattr
{
my $hostdb = shift;
my $attribute = shift;
my $section = shift;
my $type = shift;
my $condition = shift;
my $match = shift;
my $datatype = substr(lc ($type), 0, 1);
if ($datatype eq 's') {
# string
if ($condition eq 'eq') {
if (is_wildcard ($match)) {
$hostdb->findhostswithattr_strlike($attribute, $section, $match);
} else {
$hostdb->findhostswithattr_streq($attribute, $section, $match);
}
} elsif ($condition eq 'ne') {
if (is_wildcard ($match)) {
$hostdb->findhostswithattr_strnotlike($attribute, $section, $match);
} else {
$hostdb->findhostswithattr_strne($attribute, $section, $match);
}
} else {
die ("$0: string searches must be 'eq' or 'ne' - not '$condition'\n");
}
} elsif ($datatype eq 'i') {
# integer
die ("$0: integer search of non-integer value '$match'\n") if ($match !~ /^\d+$/);
if ($condition eq 'eq') {
$hostdb->findhostswithattr_inteq($attribute, $section, $match);
} elsif ($condition eq 'ne') {
$hostdb->findhostswithattr_intne($attribute, $section, $match);
} elsif ($condition eq 'lt') {
$hostdb->findhostswithattr_intlt($attribute, $section, $match);
} elsif ($condition eq 'gt') {
$hostdb->findhostswithattr_intgt($attribute, $section, $match);
} else {
die ("$0: integer searches must be 'eq', 'ne', 'gt' or 'lt' - not '$condition'\n");
}
} elsif ($datatype eq 'b') {
# blob
if ($condition eq 'eq') {
if (is_wildcard ($match)) {
$hostdb->findhostswithattr_bloblike($attribute, $section, $match);
} else {
$hostdb->findhostswithattr_blobeq($attribute, $section, $match);
}
} elsif ($condition eq 'ne') {
if (is_wildcard ($match)) {
$hostdb->findhostswithattr_blobnotlike($attribute, $section, $match);
} else {
$hostdb->findhostswithattr_blobne($attribute, $section, $match);
}
} else {
die ("$0: blob searches must be 'eq' or 'ne' - not '$condition'\n");
}
} else {
die ("$0: data type '$type' invalid");
}
}
sub is_wildcard
{
my $in = shift;
return $in =~ /%/;
}