Debian Codegraph @

contrib/ovmrtg/ovadd

raw
1 #!/usr/local/bin/perl5
2 # -*- mode: Perl -*-
3 ##################################################################
4 # This file controls the creation of all the MRTG config files from OV
5 ##################################################################
6 # Created by Laurie Gellatly <[email protected]>
7 # See newrtrov.txt for a description of what is performed
8 #################################################################
9 #
10 # Distributed under the GNU copyleft
11 #
12 # $Id: ovadd,v 1.1.1.1 2002/02/26 10:16:36 oetiker Exp $
13 #
14 use Socket;
15 use strict;
16 use vars '$DEBUG';
17 my $DEBUG = 0;
18 my $company = "Company";
19 my $rundir = "/opt/mrtg/run";
20 my $webdir = "/var/opt/ns-fasttrack/docs/mrtg/";
21 my $cronfile = "mrtgc";
22 my $roothtml = "routers.html";
23 my $buildhtf = "buildht";
24 my $dnsfile = "ovmrtgc.csv";
25
26 # Change the next line to be " &" to submit all jobs at once
27 my $backgrnd = " ";
28
29 my($at,$router,$sysn,$sysncfg,$sysnhtml,$head,$cnt);
30 my(@buildhtt,@sysipnms,@mrtgc,@sysnms);
31
32 sub main {
33
34 $router = $ARGV[0];
35 die <<USAGE unless $router;
36
37 USAGE: ovadd 'router IP ADDRESS/S'
38
39 EXAMPLE: ovadd 193.21.1.2 10.21.1.2 10.21.1.1
40
41
42 USAGE
43 chdir $rundir;
44 # Find the IP Address and SNMP sysNames from the arguments given
45 @sysipnms=`perl ovsysnms @ARGV`;
46 chomp(@sysipnms);
47 # Generate the interfaces to collect on
48 `perl ovcols + @ARGV > collects`;
49 # Start the collection happening on these interfaces
50 `xnmcollect -merge collects`;
51 `xnmcollect -event`;
52 for ($cnt = 0; $cnt < @sysipnms ; $cnt++){
53 ($router,$sysn) = split /\,/, $sysipnms[$cnt];
54 $sysn = lc($sysn);
55 $sysncfg = $sysn.".cfg";
56 $sysnhtml = $sysn.".html";
57 # Create the .cfg file for each device
58 `perl $rundir\/cfgmakerov $router > $sysncfg`;
59 # Create the root HTML file for this device in its directory
60 `perl $rundir\/index $sysncfg "Router $sysn" $sysn > $webdir$sysn\/$sysnhtml`;
61 $mrtgc[@mrtgc] = $sysn;
62 }
63
64 @mrtgc = sort(@mrtgc);
65 $cnt = 0;
66 $head = 0;
67 $at = 0;
68 open(BUILDHT, "<".$buildhtf);
69 # This next piece of code assumes that the $buildhtf file is in alpha order
70 # This code leaves old lines untouched (in case the IP address rather than
71 # the sysName was used as the target
72 while(<BUILDHT>){
73 chomp;
74 if (/index (.*)\.cfg/){
75 $sysn = $1;
76 $head = $_;
77 if (($sysn lt $mrtgc[$at]) || ($at >= @mrtgc)) {
78 $buildhtt[@buildhtt] = $head;
79 }else {
80 do{
81 $cnt = $mrtgc[$at];
82 $buildhtt[@buildhtt] =
83 "perl $rundir\/index $cnt.cfg \"Router $cnt\" $cnt > $webdir$cnt\/$cnt.html";
84 ++$at;
85 }while (($sysn ge $mrtgc[$at]) && ($at < @mrtgc));
86 if ($sysn ne $mrtgc[$at-1]){
87 $buildhtt[@buildhtt] = $head;
88 }
89 }
90 }
91 }
92 close (BUILDHT);
93 # In case there were none to start with add it the new members
94 while ($at < @mrtgc){
95 $cnt = $mrtgc[$at];
96 $buildhtt[@buildhtt] =
97 "perl $rundir\/index $cnt.cfg \"Router $cnt\" $cnt > $webdir$cnt\/$cnt.html";
98 ++$at;
99 }
100 open (BUILDHT,">".$buildhtf);
101 print BUILDHT <<ECHO;
102 #This file builds the HTML for each device being monitored
103 cd $rundir
104 ECHO
105 for ($cnt = 0; $cnt < @buildhtt ; $cnt++){
106 print BUILDHT "$buildhtt[$cnt]\n";
107 }
108 close (BUILDHT);
109
110 # Add on the new IPs and sysNames to the onces we already have
111 open (DNSFILE,"<".$dnsfile);
112 while(<DNSFILE>){
113 chomp;
114 $sysipnms[@sysipnms] = $_;
115 }
116 close (DNSFILE);
117
118 @sysipnms = sort srtipnms @sysipnms;
119 $sysn = ".";
120 for ($cnt = 0; $cnt < @sysipnms ; $cnt++){
121 $head = lc((split /\,/,$sysipnms[$cnt])[1]);
122 if($sysn eq $head){
123 next;
124 }
125 $sysn = $head;
126 $sysipnms[@sysnms] = $sysipnms[$cnt];
127 $sysnms[@sysnms] = $sysn;
128 }
129 # Now sysnms has the exact number of targets in it while sysipnms has
130 # the first @sysnms entries correct!
131
132 open (DNSFILE,">".$dnsfile);
133 for ($cnt = 0; $cnt < @sysnms ; $cnt++){
134 print DNSFILE "$sysipnms[$cnt]\n";
135 }
136 close (DNSFILE);
137
138 open (HTMLF,">".$webdir.$roothtml);
139 print HTMLF <<ECHO;
140 <HTML>
141 </HEAD>
142 <BODY BGCOLOR="#ffffff">
143 <CENTER><H1>$company Router MRTG Traffic Analysis </H1></CENTER>
144
145 <SPACER TYPE=VERTICAL SIZE=4>
146 <CENTER><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=0>
147 <TR VALIGN=top>
148 <TD WIDTH=388 ALIGN="CENTER"><FONT FACE="Arial,Helvetica" SIZE=2>
149 ECHO
150 for ($cnt = 0; $cnt < @sysnms ; $cnt++){
151 $sysn = $sysnms[$cnt];
152 $sysnhtml = $sysn.".html";
153 print HTMLF "<A HREF=\"$sysn\/$sysnhtml\">$sysn<\/A><br>\n";
154 }
155 print HTMLF <<ECHO;
156 </FONT>
157 </TD>
158 </TABLE>
159 </BODY>
160 </HTML>
161 ECHO
162 close(HTMLF);
163 open (MRTGC,">".$cronfile);
164 print MRTGC <<ECHO;
165 #This file runs the MRTG for each config $rundir/file
166 cd $rundir
167 ECHO
168 for ($cnt = 0; $cnt < @sysnms ; $cnt++){
169 print MRTGC "perl $rundir\/mrtg $sysnms[$cnt].cfg $backgrnd\n";
170 }
171 print MRTGC <<ECHO;
172 # Additional commands could go here. Under NT the next line won't return
173 $buildhtf
174 # End of rebuild ht file
175 ECHO
176 close (MRTGC);
177 # send to management station
178 # (local host unless run from a management console)
179 `ovevent "" .1.3.6.1.4.1.11.2.17.1.0.58916873 .1.3.6.1.4.1.11.2.17.2.1.0 Integer 14 .1.3.6.1.4.1.11.2.17.2.2.0 OctetString "$ENV{OVwSessionID}" .1.3.6.1.4.1.11.2.17.2.4.0 OctetString "OVMRTG ADD/Update for @ARGV COMPLETED"`
180
181 }
182 main;
183 exit(0);
184 sub srtipnms{
185 lc((split( /\,/,$a))[1]) cmp lc( (split( /\,/,$b))[1]);
186 }
187