| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use DBI; |
| 4 |
$hostname = 'localhost'; # Host that serves the mSQL Database |
| 5 |
$dbname = 'snmp'; # mySQL Database name |
| 6 |
$doit = 1; |
| 7 |
|
| 8 |
sub usage { |
| 9 |
print "$0 [-H host] [-u user] [-p password] [-v] [-h] [-n] [-d] [-a] GROUP USER EMAILADDRESS\n"; |
| 10 |
exit 0; |
| 11 |
} |
| 12 |
|
| 13 |
while ($#ARGV > -1 && $ARGV[0] =~ /^-/) { |
| 14 |
$_ = shift @ARGV; |
| 15 |
usage if (/-h/); |
| 16 |
$hostname = shift if (/-H/); |
| 17 |
$sqluser = shift if (/-u/); |
| 18 |
$pass = shift if (/-p/); |
| 19 |
$admin = 1 if (/-a/); |
| 20 |
$verbose = 1 if (/-v/); |
| 21 |
$delete = 1 if (/-d/); |
| 22 |
$doit = 0 if (/-n/); |
| 23 |
} |
| 24 |
|
| 25 |
($group, $user, $email) = @ARGV; |
| 26 |
|
| 27 |
die "group $group is a reserved group name, you can't use it. Sorry." if ($group eq "default"); |
| 28 |
|
| 29 |
die "no group specified" if (!defined($group)); |
| 30 |
|
| 31 |
( $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$hostname", $sqluser, $pass)) |
| 32 |
or die "\tConnect not ok: $DBI::errstr\n"; |
| 33 |
|
| 34 |
DO("insert into usergroups(user, groupname, isadmin) values('$user', '$group', " . (($admin) ? "'Y'" : "'N'") . ")"); |
| 35 |
if (defined($email)) { |
| 36 |
DO("insert into oncall(user, groupname, email, days, hours) values('$user', '$group', '$email', '*', '*')"); |
| 37 |
} |
| 38 |
|
| 39 |
$dbh->disconnect(); |
| 40 |
|
| 41 |
sub DO { |
| 42 |
my $cmd = shift; |
| 43 |
print $cmd,"\n" if ($verbose); |
| 44 |
$dbh->do($cmd) if ($doit); |
| 45 |
} |