Greetings,
I was just handed a spreadsheet of many servers. Rather than code them up individually, I wrote this to get the bare essentials out of the host table I keep of servers. I was careful to use CR/LF for windows rather than the traditional \n for *nix. It puts them into a previously existing sub-directory named "hosts".
------------------------------------------------
#!/usr/bin/perl
# Simple Automated zabbix_agentd.conf Generator.
use DBI;
my $dbh = DBI->connect('DBI:mysql:your_database', 'your_login', 'your_password')
or die "Couldn't connect to database: " . DBI->errstr;
my $sth = $dbh->prepare('SELECT hostname, hostip FROM List_of_Hosts')
or die "Couldn't prepare statement: " . $dbh->errstr;
# execute SELECT query
$sth->execute();
# iterate through resultset
# print values
while(my $ref = $sth->fetchrow_hashref()) {
open Configs, ">hosts/$ref->{'hostname'}_zabbix_agentd.conf";
# \015\012 used in lieu of \n for dealing with Windows CRLF issues.
print Configs "################################################# ########### \015\012";
print Configs "# Zabbix Agentd Configuration File for Windows.\015\012";
print Configs "# C:\zabbix_agentd.conf\015\012";
print Configs "# For Server: $ref->{'hostname'}\015\012";
print Configs "# By CONDOR (bofh) \015\012";
print Configs "# For more info: http://www.zabbix.com\015\012";
print Configs "# File has been stripped of normal comments for brevity.\015\012";
print Configs "################################################# ########### \015\012";
print Configs "Server=10.101.6.119\015\012";
print Configs "ServerPort=10051\015\012";
print Configs "Hostname=$ref->{'hostname'}\015\012";
print Configs "ListenPort=10050\015\012";
print Configs "ListenIP=$ref->{'hostip'}\015\012";
print Configs "StartAgents=5\015\012";
print Configs "RefreshActiveChecks=120\015\012";
print Configs "#DisableActive=1\015\012";
print Configs "#EnableRemoteCommands=1\015\012";
print Configs "DebugLevel=3\015\012";
print Configs "PidFile=C:\zabbix_agentd.pid\015\012";
print Configs "LogFile=C:\zabbix_agentd.log\015\012";
print Configs "Timeout=3\015\012";
print Configs "#--------------------------------------------------\015\012";
close(Configs);
}
# Hang up the phone, stupid.
$dbh->disconnect();
I was just handed a spreadsheet of many servers. Rather than code them up individually, I wrote this to get the bare essentials out of the host table I keep of servers. I was careful to use CR/LF for windows rather than the traditional \n for *nix. It puts them into a previously existing sub-directory named "hosts".
------------------------------------------------
#!/usr/bin/perl
# Simple Automated zabbix_agentd.conf Generator.
use DBI;
my $dbh = DBI->connect('DBI:mysql:your_database', 'your_login', 'your_password')
or die "Couldn't connect to database: " . DBI->errstr;
my $sth = $dbh->prepare('SELECT hostname, hostip FROM List_of_Hosts')
or die "Couldn't prepare statement: " . $dbh->errstr;
# execute SELECT query
$sth->execute();
# iterate through resultset
# print values
while(my $ref = $sth->fetchrow_hashref()) {
open Configs, ">hosts/$ref->{'hostname'}_zabbix_agentd.conf";
# \015\012 used in lieu of \n for dealing with Windows CRLF issues.
print Configs "################################################# ########### \015\012";
print Configs "# Zabbix Agentd Configuration File for Windows.\015\012";
print Configs "# C:\zabbix_agentd.conf\015\012";
print Configs "# For Server: $ref->{'hostname'}\015\012";
print Configs "# By CONDOR (bofh) \015\012";
print Configs "# For more info: http://www.zabbix.com\015\012";
print Configs "# File has been stripped of normal comments for brevity.\015\012";
print Configs "################################################# ########### \015\012";
print Configs "Server=10.101.6.119\015\012";
print Configs "ServerPort=10051\015\012";
print Configs "Hostname=$ref->{'hostname'}\015\012";
print Configs "ListenPort=10050\015\012";
print Configs "ListenIP=$ref->{'hostip'}\015\012";
print Configs "StartAgents=5\015\012";
print Configs "RefreshActiveChecks=120\015\012";
print Configs "#DisableActive=1\015\012";
print Configs "#EnableRemoteCommands=1\015\012";
print Configs "DebugLevel=3\015\012";
print Configs "PidFile=C:\zabbix_agentd.pid\015\012";
print Configs "LogFile=C:\zabbix_agentd.log\015\012";
print Configs "Timeout=3\015\012";
print Configs "#--------------------------------------------------\015\012";
close(Configs);
}
# Hang up the phone, stupid.
$dbh->disconnect();
Comment