📄 makecricketconfig.pl
字号:
my $ttRef = $gCT->configHash($purepath, 'graph', lc($interfacearr[$i])); if ($ttRef->{'units'}) { $units = $ttRef->{'units'}; } # Finding the datasource-type used my $dsRef = $gCT->configHash($purepath, 'datasource', lc($interfacearr[$i])); my $dstype = $dsRef->{'rrd-ds-type'}; # if there is some critical error, do this, but this should really never happen $dstype = 'DERIVE' unless $dstype; my $tempdsname = 'ds'.$i; my $dsq; if ($units) { if ($exists) { my $tempid = $bigdshash{$tempdsname}{1}; if ($tempid) { if ($interfacearr[$i] ne $bigdshash{$tempid}{'descr'} or $dstype ne $bigdshash{$tempid}{'dstype'}) { $dsq = "UPDATE rrd_datasource SET descr='$interfacearr[$i]', dstype='$dstype', units='$units' WHERE rrd_fileid=$rrd_fileid AND name='ds".$i."'"; } } } else { $dsq = "INSERT INTO rrd_datasource (rrd_fileid,name,descr,dstype,units) VALUES ($rrd_fileid,'ds".$i."','$interfacearr[$i]','$dstype','$units')"; } } else { if ($exists) { my $tempid = $bigdshash{$tempdsname}{1}; if ($tempid) { if ($interfacearr[$i] ne $bigdshash{$tempid}{'descr'} or $dstype ne $bigdshash{$tempid}{'dstype'}) { $dsq = "UPDATE rrd_datasource SET descr='$interfacearr[$i]', dstype='$dstype' WHERE rrd_fileid=$rrd_fileid AND name='ds".$i."'"; } } } else { $dsq = "INSERT INTO rrd_datasource (rrd_fileid,name,descr,dstype) VALUES ($rrd_fileid,'ds".$i."','$interfacearr[$i]','$dstype')"; } } if ($dsq) { my $r = $dbh->exec($dsq); print "\t\t$dsq\n" if $ll >= 3; unless ($r->resultStatus eq PGRES_COMMAND_OK) { #printf "ERROR DURING INSERT: %s", $dbh->errorMessage if $ll >= 2; } } else { print "\t\tNo datasource-query made.\n" if $ll >= 3; } } } else { # Gotta love perl and references... for my $i (0 .. $#{ $rrdhash{$path}{$filename}{'ds'} } ) { my $datasource = $oidhash{ @{ $rrdhash{$path}{$filename}{'ds'} }[$i] }; if ($path =~ /server/) { $datasource = @{ $rrdhash{$path}{$filename}{'ds'} }[$i]; printf "\tPath matches server, using alternate value (%s)\n", $datasource if $ll >= 3; } # Finding the units-value to give a hint for the graph my $units = 0; my $ttRef = $gCT->configHash($purepath, 'graph', lc($datasource)); if ($ttRef->{'units'}) { $units = $ttRef->{'units'}; } # Finding the datasource-type used my $dsRef = $gCT->configHash($purepath, 'datasource', lc($datasource)); my $dstype = $dsRef->{'rrd-ds-type'}; my $tempdsname = 'ds'.$i; my $dsq; if ($units) { if ($exists) { my $tempid = $bigdshash{$tempdsname}{1}; if ($tempid) { if ($datasource ne $bigdshash{$tempid}{'descr'} or $dstype ne $bigdshash{$tempid}{'dstype'}) { $dsq = "UPDATE rrd_datasource SET descr='$datasource', dstype='$dstype', units='$units' WHERE rrd_fileid=$rrd_fileid AND name='ds".$i."'"; } } } else { $dsq = "INSERT INTO rrd_datasource (rrd_fileid,name,descr,dstype,units) VALUES ($rrd_fileid,'ds".$i."','$datasource','$dstype','$units')"; } } else { if ($exists) { my $tempid = $bigdshash{$tempdsname}{1}; if ($tempid) { if ($datasource ne $bigdshash{$tempid}{'descr'} or $dstype ne $bigdshash{$tempid}{'dstype'}) { $dsq = "UPDATE rrd_datasource SET descr='$datasource', dstype='$dstype', units='$units' WHERE rrd_fileid=$rrd_fileid AND name='ds".$i."'"; } } } else { $dsq = "INSERT INTO rrd_datasource (rrd_fileid,name,descr,dstype) VALUES ($rrd_fileid,'ds".$i."','$datasource','$dstype')"; } } if ($dsq) { my $r = $dbh->exec($dsq); print "\t\t$dsq\n" if $ll >= 3; unless ($r->resultStatus eq PGRES_COMMAND_OK) { printf "ERROR DURING INSERT: %s", $dbh->errorMessage if $ll >= 2; } } else { print "\t\tNo datasource-query made.\n" if $ll >= 3; } } # end for loop } # end if ... else } # end foreach @allfiles } # end for @allpaths print "=> Done running $me <=\n" if $ll >= 2; return 1;} # end sub fillRRDdatabase# Still under developement, lacks information in the typesnmpoid to use the# standard setup.sub makeservers { my $me = "makeservers"; print "=> RUNNING $me <=\n"; my $dir = shift; &parseDefaults($dir); my $serverdir = "$cricketconfigdir/servers"; unless (-e $serverdir) { mkdir($serverdir, 0775); } my $query = "SELECT netboxid,sysname,ip,typeid,roomid,ro FROM netbox WHERE catid='SRV' ORDER BY sysname"; print "$q\n" if $ll >= 3; my $getservers = &select($dbh,$query); while (my ($id, $sysname,$ip,$type,$roomid,$community) = $getservers->fetchrow) { next unless $sysname; next unless $ip; next unless $community; print "Making targets for $id, $sysname\n" if $ll >= 3; my $os = 0; my $win32 = 'win'; my $linux = 'linux'; my $unix = 'unix'; # Check if os_guess is set, if not try to find os from category. If nothing # is found, set os to UNIX my $q = "SELECT val FROM netboxinfo WHERE netboxid=$id AND var='os_guess'"; print "\t$q\n" if $ll >= 3; my $r = $dbh->exec($q); unless ($r->ntuples > 0) { print "\tNo os_guess found for $id, $sysname\n" if $ll >= 2; $q = "SELECT category FROM netboxcategory WHERE netboxid=$id"; print "\t$q\n" if $ll >= 3; $r = $dbh->exec($q); $os = $unix; if ($r->ntuples > 0) { while (my ($category) = $r->fetchrow) { if ($category eq 'WIN') { $os = $win32; } elsif ($category eq 'LINUX') { $os = $linux; } } } else { printf "\tNo category set for %s, %s\n", $id, $sysname if $ll >= 2; } } else { my ($val) = $r->fetchrow; $os = $val; } $os = lc $os; print "\tOS set to $os\n" if $ll >= 2; my $fullpath = "$serverdir/$sysname"; $rrdhash{$fullpath}{'load'}{'netboxid'} = $id; $rrdhash{$fullpath}{'users'}{'netboxid'} = $id; $rrdhash{$fullpath}{'memory'}{'netboxid'} = $id; $rrdhash{$fullpath}{'cpu'}{'netboxid'} = $id; $rrdhash{$fullpath}{'error'}{'netboxid'} = $id; $rrdhash{$fullpath}{'processes'}{'netboxid'} = $id; my $snmpagent = 0; if($snmpagent && $snmpagent =~ /^1\.3\.6\.1\.4\.1\.311\./) { $win32 = 1; } if ($snmpagent && $snmpagent =~ /^1\.3\.6\.1\.4\.1\.8072\.3\.2\.10\./) { $linux = 1; } my $server = $sysname; mkdir ($fullpath,0775); my $fil = "$fullpath/$server"; open (FIL, ">$fil") or die "Could not open $fil for writing: $!\n"; print FIL "target --default--\n"; print FIL "\tserver\t\t=\t$sysname\n"; print FIL "\tsnmp-community\t=\t$community\n"; # Finding room description $query = "SELECT descr FROM room WHERE roomid='$roomid'"; my $getdesc = &select($dbh,$query); if (!(my $desc = $getdesc->fetchrow)) { print "\tNo description for room with id=$roomid\n"; print FIL "\tshort-desc\t=\t\"\"\n"; } else { print FIL "\tshort-desc\t=\t\"$desc\"\n"; } print FIL "\n"; # USERS my $ds; if($os eq $win32) { $ds = "userswin"; } else { $ds = "usersnix"; } $rrdhash{$fullpath}{'users'}{'ds'} = $rtargetoidhash{$ds}; print FIL "target \"users\"\n"; print FIL "\ttarget-type\t=\t$ds\n\n"; # PROCESSES print FIL "target \"processes\"\n"; print FIL "\ttarget-type\t=\tprocesses\n\n"; $rrdhash{$fullpath}{'processes'}{'ds'} = $rtargetoidhash{'processes'}; # LOAD if($os ne $win32) { print FIL "target \"load\"\n"; print FIL "\ttarget-type\t=\tloadnix\n\n"; $rrdhash{$fullpath}{'load'}{'ds'} = $rtargetoidhash{'loadnix'}; } # MEMORY if($os eq $win32) { $ds = "memwin"; } elsif($os eq $linux) { $ds = "memlin"; } else { $ds = "memnix"; } print FIL "target \"memory\"\n"; print FIL "\ttarget-type\t=\t$ds\n\n"; $rrdhash{$fullpath}{'memory'}{'ds'} = $rtargetoidhash{$ds}; # CPU if($os eq $win32) { $ds = "cpuwin"; } else { $ds = "cpunix"; } print FIL "target \"cpu\"\n"; print FIL "\ttarget-type\t=\t$ds\n\n"; $rrdhash{$fullpath}{'cpu'}{'ds'} = $rtargetoidhash{$ds}; # ERROR if($os eq $win32) { print FIL "target \"Error\"\n"; print FIL "\ttarget-type\t=\terror\n\n"; $rrdhash{$fullpath}{'error'}{'ds'} = $rtargetoidhash{'error'}; } close FIL; # INTERFACES # We make separate directory for interfaces $query = "SELECT key FROM netboxinfo WHERE var='interf_type' AND netboxid=$id"; my $getinterfaces = &select($dbh,$query); if ($getinterfaces->ntuples > 0) { mkdir ("$fullpath/interface",0775); $fil = "$fullpath/interface/interfaces"; open (FIL, ">$fil") or die "Could not open $fil for writing: $!\n"; print FIL "target --default--\n"; print FIL "\tserver\t\t=\t$sysname\n"; print FIL "\ttarget-type\t=\tinterface\n"; print FIL "\tinst\t\t=\tmap(interface-name)\n"; print FIL "\n"; while (my $interface = $getinterfaces->fetchrow) { my $name = $interface; $name =~ s/\s/_/g; # We need filesystem-rrd-nicer names without spaces $name =~ s,/,_,g; my $interface2 = $interface; $interface2 =~ s/\\/\\\\/g; print FIL "target \"$name\"\n"; print FIL "\tinterface-name\t=\t\"$interface2\"\n"; print FIL "\tshort-desc\t=\t\"$interface\"\n"; print FIL "\n"; $rrdhash{"$fullpath/interface"}{$name}{'netboxid'}= $id; $rrdhash{"$fullpath/interface"}{$name}{'ds'} = $rtargetoidhash{"interface"}; } close FIL; } # DISKS # Then all the disks $query = "SELECT key,val FROM netboxinfo WHERE var='disk_blocksizeInt' AND netboxid=$id"; my $getpaths = &select($dbh,$query); if ($getpaths->ntuples > 0) { mkdir ("$fullpath/disk",0775); $fil = "$fullpath/disk/disks"; open (FIL, ">$fil") or die "Could not open $fil for writing: $!\n"; print FIL "target --default--\n"; print FIL "\tserver\t\t=\t$sysname\n"; print FIL "\ttarget-type\t=\tdisk\n"; print FIL "\tinst\t\t=\tmap(mount-point)\n"; print FIL "\n"; while (my ($key,$val) = $getpaths->fetchrow) { my $name = $key; if($name eq "/") { $name = "root"; # Special case for / } else { $name =~ s,/,_,g; # /usr/local -> _usr_local $name =~ s/^_//; # _usr -> usr $name =~ s/:.*//; # C:\ Label nblablabalb -> C } $key =~ s/\\/\\\\/g; # Double escape backslashes in configfile C:\ --> C:\\ print FIL "target \"$name\"\n"; # This is for diskIO useage in the future #if ($solaris) { # print FIL "\ttarget-type = disksolaris\n"; } print FIL "\tmount-point\t=\t\"$key\"\n"; print FIL "\tshort-desc\t=\t\"$key\"\n"; print FIL "\tblocksize\t=\t$val\n"; print FIL "\n"; $rrdhash{"$fullpath/disk"}{$name}{'netboxid'}= $id; $rrdhash{"$fullpath/disk"}{$name}{'ds'} = $rtargetoidhash{"disk"}; } close FIL; } } }# Sorting the dsub makeView { my @oids = @_; # Ok, we have a list of oids to put into groups based on the # %viewarr. We will return a hash where the groupnames are # keys and the members of the group is a reference to an array my @strings; foreach my $group (sort keys %viewarr) { my @temp = (); foreach my $groupmember (@ { $viewarr{ $group } }) { foreach my $oid (@oids) { if ($oidhash{$oid} eq $groupmember) { push @temp, $groupmember; printf "Pushing %s on %s\n", $groupmember, $group; } } } if (@temp) { push @strings, "$group:". join (" ", @temp); } } my $returnstring = "\tview\t= \"" . join(", ", @strings) . "\""; print "$returnstring\n"; return $returnstring;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -