Debian Codegraph @

examples/rahosts/archive/rahostsdaily.pl.in

raw
1 #!@PERLBIN@
2 #
3 # Argus-5.0 Client Software. Tools to read, analyze and manage Argus data.
4 # Copyright (c) 2000-2024 QoSient, LLC
5 # All rights reserved.
6 #
7 # THE ACCOMPANYING PROGRAM IS PROPRIETARY SOFTWARE OF QoSIENT, LLC,
8 # AND CANNOT BE USED, DISTRIBUTED, COPIED OR MODIFIED WITHOUT
9 # EXPRESS PERMISSION OF QoSIENT, LLC.
10 #
11 # QOSIENT, LLC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
12 # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 # AND FITNESS, IN NO EVENT SHALL QOSIENT, LLC BE LIABLE FOR ANY
14 # SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
16 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
17 # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
18 # THIS SOFTWARE.
19 #
20 #
21 # rahostsdaily - run rahosts to update the daily inventory tables.
22 #
23 # $Id: //depot/gargoyle/clients/examples/rahosts/rahosts.pl#5 $
24 # $DateTime: 2014/10/07 15:00:33 $
25 # $Change: 2938 $
26 #
27 #
28 # Complain about undeclared variables
29 use v5.6.0;
30 use strict;
31
32 local $ENV{PATH} = "$ENV{PATH}:/bin:/usr/bin:/usr/local/bin";
33
34 # Used modules
35 use POSIX;
36 use POSIX qw(strftime);
37 use URI::URL;
38 use DBI;
39 use Switch;
40 use Net::IP;
41 use File::Which qw/ which /;
42 use Socket;
43
44 use File::Temp qw/ tempfile tempdir /;
45 use File::Which qw/ which where /;
46 use qosient::XS::util; # parsetime
47
48 # Global variables
49 my $VERSION = "5.0.3";
50 my $done = 0;
51 my $debug = 0;
52 my $drop = 0;
53 my $time;
54 my $database;
55
56 my ($val, $stime, $etime);
57 my ($user, $pass, $host, $port, $space, $db, $table);
58 my $dbh;
59 my $sth;
60
61 my $uri = 0;
62 my $scheme;
63 my $netloc;
64 my $path;
65
66 my $datename = "";
67 my @results = ();
68 my $results_ref = \@results;
69 my $elements = "";
70 my $object = "";
71
72 my $ra;
73 my $rahosts = which 'rahosts';
74
75 my @dates;
76
77 my $f;
78 my $fname;
79 ($f, $fname) = tempfile();
80
81 # Parse the arguments if any
82
83 my @arglist;
84 ARG: while (my $arg = shift(@ARGV)) {
85 if (!$done) {
86 for ($arg) {
87 s/^-D$// && do { $debug = shift(@ARGV); next ARG; };
88 s/^-debug$// && do { $debug++; next ARG; };
89 s/^-drop// && do { $drop = 1; next ARG; };
90 s/^-dbase$// && do { $database = shift(@ARGV); next ARG; };
91 s/^-t$// && do { $time = shift(@ARGV); next ARG; };
92 s/^-time$// && do { $time = shift(@ARGV); next ARG; };
93 s/^-w// && do { $uri = shift (@ARGV); next ARG; };
94 }
95 } else {
96 for ($arg) {
97 s/\(/\\\(/ && do { ; };
98 s/\)/\\\)/ && do { ; };
99 }
100 }
101 $arglist[@arglist + 0] = $arg;
102 }
103
104 if ((not defined $time) || ($time eq "-1d") || ($time eq "Today")) {
105 $time = RaTodaysDate();
106 }
107
108 RaHostsDailyProcessParameters($fname);
109 RaHostsDailyRunRoutines($fname);
110 RaHostsDailyCleanUp($fname);
111 exit;
112
113
114 sub RaHostsDailyProcessParameters {
115 if (defined $time) {
116 ($val, $stime, $etime) = qosient::XS::util::ArgusParseTime($time);
117 print "DEBUG: RaCalendarProcessParameters: time:'$time' val:'$val' stime:'$stime' etime:'$etime'\n" if $debug;
118
119 @dates = RaHostsDailyGetDates($stime,$etime);
120 }
121 }
122
123 sub RaHostsDailyRunRoutines {
124 for my $i (0 .. $#dates) {
125 my $time = $dates[$i];
126 my $dbdate = $time;
127 $dbdate =~ s/\//_/g;
128 my $ra = "$rahosts -F /usr/argus/rarc -t $time -w mysql://root\@localhost/hostsInventory/host_$dbdate";
129
130 if ($debug > 1) {
131 $ra .= " -D3";
132 }
133 print "DEBUG: calling: $ra\n" if $debug;
134
135 if (system($ra) != 0) {
136 print "rahostsdaily: error: $ra failed\n";
137 exit -1;
138 }
139 }
140 }
141
142
143 sub RaHostsDailyCleanUp {
144 my $file = shift;
145 print "DEBUG: deleting '$file'\n" if $debug;
146 unlink $file;
147 exit 0;
148 }
149
150
151 sub RaHostsDailyGetDates {
152 my $stime = shift;
153 my $etime = shift;
154 my @dates = ();
155
156 print "DEBUG: RaHostsDailyGetDates: stime $stime etime $etime\n" if $debug;
157
158 while ($stime < $etime) {
159 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($stime);
160 my $date = sprintf("%4d/%02d/%02d", $year+1900, $mon + 1, $mday);
161 push @dates, $date;
162 $stime += 86400;
163 }
164
165 my $dlen = scalar @dates;
166 print "DEBUG: RaHostsDailyGetDates: found $dlen values\n" if $debug;
167 return @dates;
168 }
169
170 sub RaTodaysDate {
171 my($day, $month, $year)=(localtime)[3,4,5];
172 return sprintf( "%04d/%02d/%02d", $year+1900, $month+1, $day);
173 }