Commit 727c6385 authored by anton's avatar anton
Browse files

remove campuswlan icinga config script

parent 8879f8b2
#!/usr/bin/perl
# Dieser Dienst konfiguriert die APs auf dem lokalen Icinga
use strict;
use warnings;
use DBI;
use DBD::mysql;
use Getopt::Long;
use Sys::Syslog qw(:standard :macros); # standard functions & macros
use File::Slurp;
use Sys::Prctl qw(prctl);
use Socket;
# http://www.nntp.perl.org/group/perl.perl5.porters/2004/04/msg91204.html
$SIG{'PIPE'} = 'IGNORE';
my $db_host = "10.42.1.27";
my $db_user = "icingaconfd";
my $db_db = "fembot";
my $debug = 0;
my @ignoreHosts = (); # won't get deleted locally
my $path = "/etc/icinga2/conf.d/wlanlinks/campus/autoconf";
#my $spath = "/etc/icinga2/conf.d/radiolinks/switch";
my $DBPWFILE = "/root/icingaconfreadpw.txt";
#my $restartIcinga = "sudo /etc/init.d/icinga2 reload";
my $dns1030 = "wlan.net.fem.tu-ilmenau.de";
my $dns1045 = "wlan.fem.tu-ilmenau.de";
GetOptions (
"dbpwfile=s" => \$DBPWFILE, # string
"dbhost=s" => \$db_host, # string
"dbuser=s" => \$db_user, # string
"dbdb=s" => \$db_db, # string
"icingapath=s" => \$path, # string
# "icingapathswitch=s" => \$spath, # string
"dns1030=s" => \$dns1030, # string
"dns1045=s" => \$dns1045, # string
"verbose" => \$debug ); # flag
open(FILE,"<$DBPWFILE");
my ($DB_READ_STRING, undef) = <FILE>;
close(FILE);
$DB_READ_STRING = 'test' unless defined $DB_READ_STRING; # sollte für get-Anfragen benutzt werden
$DB_READ_STRING =~ s/[\n\r]//g;
openlog("icingaconfd", 'perror,pid', LOG_DAEMON);
reconf_icinga();
closelog;
sub sql_error_handler {
my $errstr = shift;
my $dbh = shift;
my $query = $dbh->{Statement};
my $full_errstr = 'SQL_ERROR: ' . $errstr . ', query: ' . $query;
syslog(LOG_ERR, "DB ERROR: $full_errstr");
return 1; # Stops RaiseError
}
sub generateContent {
my ($nick, $ip1030, $ip1045, $switchname) = @_;
my $content = <<END_CONTENT;
object Host "$nick" {
import "wlan-campus"
address = "$ip1045"
vars.net_address = "$ip1030"
END_CONTENT
if (defined($switchname)) {
$content .= <<END_CONTENT;
vars.parent = "$switchname"
END_CONTENT
}
$content .= <<END_CONTENT;
vars.host_class = "ap-campus-wlan"
#
# vars.ping["$ip1030"] = {description = "$nick.net"}
}
END_CONTENT
return $content;
}
sub reconf_icinga
{
syslog(LOG_INFO, sprintf("%s: running reconf_icinga\n", getnow())) if $debug;
my $dbh = DBI->connect("dbi:mysql:database=$db_db;host=$db_host", $db_user, $DB_READ_STRING, {AutoCommit => 0, RaiseError => 1, HandleError => \&sql_error_handler}) or die("connect to db failed");
my $sth = $dbh->prepare("SELECT nick, (SELECT switchname FROM `radius`.`aps_location` apsl WHERE apsl.apname = aps.nick) as switchname FROM `aps` WHERE lastseen IS NOT NULL AND (TIMESTAMPDIFF(DAY,lastseen,CURRENT_TIMESTAMP) < 30);"); # all APs that were online within the last 30d
$sth->execute();
# Verarbeite jeden AP
my $modified = 0;
my %seen = ();
while(my $hash_ref = $sth->fetchrow_hashref) {
my $nick = ${$hash_ref}{"nick"};
my $switch = ${$hash_ref}{"switchname"};
next unless $nick =~/^[a-z0-9]+$/;
next unless (not defined($switch)) or ($switch =~/^[a-z0-9_-]+$/);
$seen{$nick} = 1;
my $ip1045 = inet_aton($nick.".".$dns1045);
next unless $ip1045;
$ip1045 = inet_ntoa($ip1045);
my $ip1030 = inet_aton($nick.".".$dns1030);
next unless $ip1030;
$ip1030 = inet_ntoa($ip1030);
my $newcontent = generateContent($nick, $ip1030, $ip1045, $switch);
my $file = $path."/".$nick.".conf";
if (-e $file) {
my $content = read_file($file);
if ($content ne $newcontent) {
syslog(LOG_INFO, sprintf("%s: reconf_icinga: %s: rewritten from db", getnow(), $nick));
write_file($file, $newcontent);
$modified++;
}
} else {
syslog(LOG_INFO, sprintf("%s: reconf_icinga: %s: created from db", getnow(), $nick));
write_file($file, $newcontent);
$modified++;
}
}
$dbh->commit();
$dbh->disconnect();
my @FILES = read_dir($path);
foreach my $file (@FILES) {
$file =~s/\.conf$//;
next if $seen{$file};
syslog(LOG_INFO, sprintf("%s: reconf_icinga: %s: disabled as missing in db", getnow(), $file));
unlink($path."/".$file.".conf");
$modified++;
}
syslog(LOG_INFO, sprintf(("%s: reconf_icinga done\n", getnow()))) if $debug;
}
sub getnow
{
my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
return sprintf("%02d:%02d:%02d ",$hour,$minute,$second);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment