-
Notifications
You must be signed in to change notification settings - Fork 9
/
smt-client
executable file
·281 lines (212 loc) · 7.62 KB
/
smt-client
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/perl -w
###############################################################################
## Copyright (c) 2009-2012 SUSE LINUX Products GmbH, Nuernberg, Germany.
###############################################################################
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use Text::ASCIITable;
use Locale::gettext();
use POSIX();
use SMT::CLI;
use SMT::Utils;
use SMT::Client;
POSIX::setlocale(&POSIX::LC_MESSAGES, "");
if(!SMT::Utils::dropPrivileges())
{
print STDERR __("Unable to drop privileges. Abort!\n");
exit 1;
}
my $vblevel = LOG_ERROR|LOG_WARN|LOG_INFO1|LOG_INFO2;
my $debug = 0;
my $logfile = "/dev/null";
my $help = 0;
# No command defined, use fallback
my $command = $ARGV[0] || '';
my $hostname = undef;
my $verbose = 0;
my $guid = undef;
my $severity = undef;
Getopt::Long::Configure( 'no_auto_abbrev');
my $optres = Getopt::Long::GetOptions
(
'help|h' => \$help,
'debug|d' => \$debug,
'logfile|L=s' => \$logfile,
'hostname|n=s' => \$hostname,
'verbose|v' => \$verbose,
'guid|g=s' => \$guid,
'severity|s=s' => \$severity
);
# No command defined, but some arguments are, using fallback
if (defined $command && $command =~ /^-/)
{
$command = '';
}
if($help || !$optres)
{
print basename($0). " status [--hostname <hostname>]\n";
print "\n";
print __("Options:")."\n";
print "--help -h ".__("This help.")."\n";
print "--debug -d ".__("Enable debug mode.")."\n";
print "--hostname -n <name> ".__("Filter the result by the hostname (hostname starting with <name>).")."\n";
print "--guid -g <guid> ".__("Only show information for GUID <guid>.")."\n";
print "--severity -s <level> ".__("Only show information if patches for <level> are available.")."\n";
print " ".__("<level> may be a value of: 'packagemanager', 'security', 'recommended' or 'optional'")."\n";
print "--verbose -v ".__("Show details on patches.")."\n";
print "--logfile -L ".__("Log to a specified file.")."\n";
exit 0;
}
# get a lock
#if(!SMT::Utils::openLock("smt-client"))
#{
# print __("Other smt-client process is still running.\n");
# exit 2;
#}
# open the logfile
$vblevel = LOG_ERROR|LOG_WARN|LOG_INFO1|LOG_INFO2|LOG_DEBUG|LOG_DEBUG2 if($debug);
my $log = SMT::Utils::openLog($logfile);
# connect to database
my ($cfg, $dbh) = SMT::CLI::init();
if (! defined $dbh) {
print __("Cannot connect to database");
exit 3;
}
my $clients = SMT::Client->new({'dbh' => $dbh});
sub doesHostnameMatch ($) {
my $check_hostname = shift;
return (substr ($check_hostname, 0, length($hostname)) eq $hostname);
}
if ($command eq '' || $command eq 'status')
{
my $serverityFilter = undef;
if ( defined $severity )
{
$serverityFilter = '>0:::' if ( $severity eq 'packagemanager');
$serverityFilter = ':>0::' if ( $severity eq 'security');
$serverityFilter = '::>0:' if ( $severity eq 'recommended');
$serverityFilter = ':::>0' if ( $severity eq 'optional');
if ( ! defined $serverityFilter )
{
print __("Unknown severity level.")."\n";
exit 2;
}
}
my $filter = { 'selectAll' => '' };
${$filter}{'GUID'} = $guid if ( defined $guid && $guid ne '' );
${$filter}{'PATCHSTATUS'} = $serverityFilter if ( defined $serverityFilter );
my $client_info = $clients->getClientsInfo($filter);
# skip rendering if an empty hash is detected
if ( keys %{$client_info})
{
my $t = new Text::ASCIITable;
if ($verbose) {
$t->setCols(
__("GUID"),
__("Hostname"),
__("Patch Status"),
__("Security\nPatches"),
__("Package Manager\nPatches"),
__("Recommended\nPatches"),
__("Optional\nPatches"),
__("Patch Status Date"),
__("Last Contact")
);
} else {
$t->setCols(
__("GUID"),
__("Hostname"),
__("Patch Status"),
__("Patch Status Date")
);
}
my ($key, $status_label);
foreach my $key (sort ({$client_info->{$a}->{HOSTNAME} cmp $client_info->{$b}->{HOSTNAME}} keys %{$client_info})) {
next if (defined $hostname && ! doesHostnameMatch($client_info->{$key}->{HOSTNAME}));
# Function returns two values
($status_label) = SMT::Client::getPatchStatusLabel($client_info->{$key});
if ($verbose) {
$t->addRow(
$client_info->{$key}->{GUID},
$client_info->{$key}->{HOSTNAME},
$status_label,
(defined $client_info->{$key}->{PATCHSTATUS_S} ? $client_info->{$key}->{PATCHSTATUS_S}:__("Unknown")),
(defined $client_info->{$key}->{PATCHSTATUS_P} ? $client_info->{$key}->{PATCHSTATUS_P}:__("Unknown")),
(defined $client_info->{$key}->{PATCHSTATUS_R} ? $client_info->{$key}->{PATCHSTATUS_R}:__("Unknown")),
(defined $client_info->{$key}->{PATCHSTATUS_O} ? $client_info->{$key}->{PATCHSTATUS_O}:__("Unknown")),
$client_info->{$key}->{PATCHSTATUS_DATE},
$client_info->{$key}->{LASTCONTACT}
);
} else {
$t->addRow(
$client_info->{$key}->{GUID},
$client_info->{$key}->{HOSTNAME},
$status_label,
$client_info->{$key}->{PATCHSTATUS_DATE}
);
}
}
print $t->draw();
}
}
else
{
SMT::Utils::printLog($log, $vblevel, LOG_ERROR, sprintf(__("Unknown command: %s"), $command));
}
# release the lock
#SMT::Utils::unLockAndExit( "smt-client", 0, $log, $vblevel );
exit 0;
########################################################################################
#
# Manpage
#
=head1 NAME
smt-client
=head1 SYNOPSIS
smt-client [COMMAND] [OPTION [OPTION ...]]
=head1 DESCRIPTION
C<smt-client> shows information about registered clients. The information includes the GUID, hostname, patch status and the timestamps of the patch status and the last contact of the client with the SMT server.
=head1 COMMAND
Currently only the command B<status> is supported. It is the default and can be omitted.
=head1 OPTIONS
=head2 BASIC OPTIONS
=over
=item -h, --help
Shows the help screen.
=item -v, --verbose
Shows detailed client information.
The patch status is explained in detail and shows the number of patches for the package manager, security, recommended and optional patches. The last contact date is also shown in verbose mode.
=item -d, --debug
Enables debug mode.
=item -L, --logfile <file>
Logs will be written to <file>.
=back
=head2 FILTER OPTIONS
=over
=item -h, --hostname <name>
Filters the result by the hostname. Only entries with a hostname beginning with <name> will be listed.
=item -g, --guid <guid>
Filters the result by the GUID. Only the entry with the GUID <guid> will be listed.
=item -s, --severity <level>
Filters the result by the patch status information. The value <level> may be one of B<packagemanager>, B<security>, B<recommended> or B<optional>.
Only entries are listed that have patches for the respective level.
=back
=cut
=head1 AUTHORS and CONTRIBUTORS
Lukas Ocilka, Jens Daniel Schmidt
=cut
=head1 LICENSE
Copyright (c) 2009-2012 SUSE LINUX Products GmbH, Nuernberg, Germany.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.
=cut