-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrtg_map.pl
120 lines (92 loc) · 3.07 KB
/
mrtg_map.pl
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
#!/usr/bin/perl
# Criar arquivos de configuracoes mrtg dinamicamente, efetuando associacao
# entre fs's antigos e criando configuracoes para novos fs's
################################
# 2007/10/23 - pmoranga
#
use File::Copy;
use warnings;
use strict;
use Getopt::Std;
my %options = ();
getopts("d",\%options);
my $mrtgconf="/etc/mrtg/mrtg_nas.cfg";
my $mapconf="/export/scripts/mrtg_map.conf";
my $mrtg_work_dir="/export/mrtg/data";
my @newconf;
####### Arquivo de entrada ($CONF) ##########################
##new_fs old_fs
#/mnt/fs/to_graph
#/mnt/fs/another_fs_to_map_new /mnt/fs/another_fs_to_map_old
debug ( "mapconf: ". -C $mapconf );
debug ( "mrtgconf: ". -C $mrtgconf );
if ( ( -C $mapconf ) > ( -C $mrtgconf ) ) {
print "Don't need update $mrtgconf newer than $mapconf!\n";
exit 1;
}
open (CONF,"<$mapconf") or die "argh!! can't open $mapconf\n";
push(@newconf,<DATA>);
foreach my $line (<CONF>){
chomp $line;
if ( $line =~ m@^(/\S+)(\s+(/\S+)?\s*)?$@ ){
push(@newconf, generate_conf($1,$3) );
}
}
close (CONF);
open (MCFG,">$mrtgconf") or die "Can't open $mrtgconf for write" ;
foreach my $l (@newconf){
print MCFG $l . "\n";
}
close (MCFG);
`indexmaker --title="Graficos FS" --section=title --show=month /etc/mrtg/mrtg_nas.cfg --pageend="<p>Pagina gerada dinamicamente pelo script /export/scripts/mrtg_map.pl, em \`date\`. Graficos antigos em <A HREF=/nas/html/>/nas/html/</A></p><BR><BR>" > /export/mrtg/nas/html/nas.html`;
exit 0;
# funcoes
sub generate_conf{
my $fs = $_[0];
my $oldfs = $_[1] if defined $_[1];
my @conf;
# caso datafile novo nao exista e exista antigo, copiar dados
if ( ! -e datafile($fs) && defined $oldfs){
copy datafile($oldfs), datafile($fs);
}
# recria arquivo de configuracao
my $id = getId($fs);
my $share = `df -P $fs | grep $fs | awk '{print \$1}'`;
chomp $share;
debug("id: $id fs: $fs share: $share");
push(@conf,'## Autogenerated graph for '.$fs.' ##');
push(@conf,'Target['.$id.']: `/export/scripts/mrtg_df.sh '.$fs.'`');
push(@conf,'MaxBytes['.$id.']: 99999999');
push(@conf,'Title['.$id.']: '.$share.' - Usage Area');
push(@conf,'PageTop['.$id.']: Area utilizada e area total do filesystem '.
$share.' montado em '.$ENV{HOSTNAME}.':'.$fs.'');
push(@conf,'PNGTitle['.$id.']: Space '.$share.'' );
return @conf;
}
sub datafile{
my $dbf = $mrtg_work_dir."/".getId($_[0]).".log";
debug($dbf);
return $dbf;
}
sub getId{
my $id = $_[0];
$id =~ s@/@_@g;
debug("id: $id");
return $id;
}
sub debug{
print "$_[0]\n" if $options{d};
}
__DATA__
HtmlDir: /export/mrtg/nas/html
ImageDir: /export/mrtg/nas/image
LogDir: /export/mrtg/data
ThreshDir: /var/lib/mrtg
WithPeak[_]: wmy
Options[_]: nopercent,transparent,gauge,growright
kMG[_]: G,T,P
ShortLegend[_]: B
YLegend[_]: GBytes
LegendI[_]: Used:
LegendO[_]: Total:
MaxBytes[_]: 99999999