Advertisement
Guest User

GarciaPL

a guest
Aug 22nd, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 9.32 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. ######################################
  3. # Info : Add User Nagios on st1monms2
  4. # Version : 1.0
  5. # Date : 7 luty 2012
  6. # Author : Lukasz Ciesluk
  7. # Help : http://pl.linkedin.com/in/lukaszciesluk/
  8. ######################################
  9. #
  10. # Run :
  11. # chmod +x AddUserNagios_st1monms2.pl
  12. # ./AddUserNagios_st1monms2.pl -h (for help)
  13. # ./AddUserNagios_st1monms2.pl -u <NEW_USERNAME> --username_db <DATABASE_USERNAME> --password_db <DATABASE_PASSWORD> --database <DATABASE_NAME>
  14.  
  15. use strict;
  16. use warnings;
  17. use Getopt::Long;
  18. use DBI;
  19. use Tie::File;
  20.  
  21. my $new_nagios_user = undef;
  22. my $username_db = undef;
  23. my $password_db = undef;
  24. my $database = undef;
  25. my $sql = undef;
  26. my $dbh = undef;
  27. my $help = undef;
  28.  
  29. my $htpasswd_directory = "/opt/nagios/etc/";
  30. my $htpasswd_file = "htpasswd.users";
  31. my $nagios_cgi_directory = "/opt/nagios/etc/";
  32. my $nagios_cgi_cfg_file = "cgi.cfg";
  33. my @nagios_cgi_fields = ( 'authorized_for_all_services', 'authorized_for_all_hosts', 'authorized_for_read_only' );
  34.  
  35. my $domain = "\@BANK.COM.PL";
  36. my $comma = ",";
  37. my $password_algorithm = "b64_sha1";
  38. my $password_user_merlin = "4insgmC8hl++J1uTcPVCtfo2uX0=";
  39.  
  40. sub usage {
  41.     print "$0 --u <new_nagios_user> --dbu <username_db> --dbp <password_db> --dbd <database>\n";
  42. }
  43.  
  44. sub connect_db_merlin {
  45.     $dbh = DBI->connect("dbi:mysql:database=$database;"."host=localhost;port=3306", $username_db, $password_db) || die "Nie mozna sie polaczyc do bazy: $DBI::errstr";
  46. }
  47.  
  48. sub duplicate_htpasswd {
  49.     my ($nagios_user_create) = @_;
  50.     if (-d $htpasswd_directory) {
  51.         chdir($htpasswd_directory) or die "Can not change directory to the $htpasswd_directory!";
  52.        
  53.         my $cat_user = `cat $htpasswd_file | grep $nagios_user_create`;
  54.         if (length($cat_user) > 0) {
  55.             print "User has record in $htpasswd_file file which may mean that user can exists. Exit program\n";
  56.             exit;
  57.         }
  58.        
  59.         tie my @array, 'Tie::File', $htpasswd_file or die "Reading file $htpasswd_file finished with an error : $!\n";
  60.         my $monkey_index = index($array[-1], '@');
  61.         my $domain_substring = substr($array[-1], $monkey_index, length($array[-1]));
  62.         my $new_user_string = $nagios_user_create.$domain_substring;
  63.         print "Adding record to $htpasswd_file file : $new_user_string\n";
  64.         open (FILE, ">>$htpasswd_file") || die "Error opening file $htpasswd_file : $!\n";
  65.         print FILE "$new_user_string\n";
  66.         close FILE;
  67.     } else {
  68.         print "Catalog $htpasswd_directory does not exist. Exit program\n";
  69.         exit;
  70.     }
  71. }
  72.  
  73. sub duplicate_st1monms2_cgi {
  74.     my ($nagios_user_create) = @_;
  75.     if (-d $nagios_cgi_directory) {
  76.         chdir($nagios_cgi_directory) or die "Can not change directory to the $nagios_cgi_directory!";
  77.        
  78.         my $cat_user = `cat $nagios_cgi_cfg_file | grep $nagios_user_create`;
  79.         if (length($cat_user) > 0) {
  80.             print "User has record in $htpasswd_file file which may mean that user can exists. Exit program\n";
  81.             exit;
  82.         }
  83.  
  84.         open ( FILE, "$nagios_cgi_cfg_file" ) || die "Error opening file $nagios_cgi_cfg_file : $!\n";
  85.         my @lines = <FILE>;
  86.         for my $linia (@lines) {
  87.             foreach my $field (@nagios_cgi_fields) {
  88.                 if($linia =~ /$field/){
  89.                     $linia =~ s/^\s+//;
  90.                     $linia =~ s/\s+$//;
  91.                     $linia =~ s/^\s+//;
  92.                     $linia =~ s/\s+$//;
  93.                    
  94.                     my @add_user_authorizate = `sed -i.bak -e s/$linia/$linia$comma$nagios_user_create$domain/g $nagios_cgi_cfg_file`;
  95.                     print @add_user_authorizate;
  96.                    
  97.                     print "Granted $field privilege to user $nagios_user_create\n";
  98.                 }
  99.             }
  100.         }
  101.         close (FILE);
  102.     } else {
  103.         print "Catalog $nagios_cgi_directory does not exist. Exit program\n";
  104.         exit;
  105.     }
  106. }
  107.  
  108. sub restart_nagios {
  109.     my @restart = `mon restart`;
  110.     print @restart;
  111. }
  112.  
  113. sub merlin_db_operations {
  114.     my ($nagios_user_create) = @_;
  115.    
  116.     print "Checking if user $nagios_user_create$domain exists in database already\n";
  117.     my $sthUserExists = $dbh->prepare('select count(*) as ilosc from users where username = ?') || die "Database Select User Error $DBI::errstr";
  118.     $sthUserExists->bind_param(1, $nagios_user_create.$domain);
  119.     $sthUserExists->execute();
  120.     my $ifexists = $sthUserExists->fetchrow_hashref();
  121.    
  122.     if ($ifexists->{ilosc} > 0) {
  123.         print "User $nagios_user_create$domain exists! Interrupt program!\n";
  124.         $dbh->disconnect();
  125.         exit;
  126.     } else {
  127.         print "User $nagios_user_create$domain does not exist in database. Continuing.\n";
  128.     }
  129.  
  130.     my $sth = $dbh->prepare('select max(id) as maxid from users') || die "Database Select Max ID Error $DBI::errstr";
  131.     $sth->execute();
  132.     my $result = $sth->fetchrow_hashref();
  133.     my $new_user_id = $result->{maxid} + 1;
  134.  
  135.     print "Please give name of user (realname)\n";
  136.     my $realname = <>;
  137.     chomp ($realname);
  138.     print "Please give e-mail address of user (e-mail)\n";
  139.     my $email = <>;
  140.     chomp ($email);
  141.    
  142.     my $sthUserTable = $dbh->prepare('insert into users(id, realname, email, username, password_algo, password) VALUES (?, ?, ?, ?, ?, ?)');
  143.     $sthUserTable->bind_param(1, $new_user_id);
  144.     $sthUserTable->bind_param(2, $realname);
  145.     $sthUserTable->bind_param(3, $email);
  146.     $sthUserTable->bind_param(4, $nagios_user_create.$domain);
  147.     $sthUserTable->bind_param(5, $password_algorithm);
  148.     $sthUserTable->bind_param(6, $password_user_merlin);
  149.     $sthUserTable->execute();
  150.    
  151.     print "Added to Users table a user (username) : $nagios_user_create$domain with ID = $new_user_id\n";
  152.     print "Selected algorithm for password : $password_algorithm and password : $password_user_merlin\n";
  153.    
  154.     print "Adding roles for user\n";
  155.     my $sthRolesUsersTable = $dbh->prepare('insert into roles_users(user_id, role_id) VALUES (?, 1)');
  156.     $sthRolesUsersTable->bind_param(1, $new_user_id);
  157.     $sthRolesUsersTable->execute();
  158.    
  159.     print "Please answer for user roles : \n";
  160.     print "Add system_information role ? (0 - no, 1 - yes)\n";
  161.     my $system_information = <>;
  162.     chomp ($system_information);
  163.     print "Add configuration_information role ? (0 - no, 1 - yes)\n";
  164.     my $configuration_information = <>;
  165.     chomp ($configuration_information);
  166.     print "Add system_commands role ? (0 - no, 1 - yes)\n";
  167.     my $system_commands = <>;
  168.     chomp ($system_commands);
  169.     print "Add all_services role ? (0 - no, 1 - yes)\n";
  170.     my $all_services = <>;
  171.     chomp ($all_services);
  172.     print "Add all_hosts role ? (0 - no, 1 - yes)\n";
  173.     my $all_hosts = <>;
  174.     chomp ($all_hosts);
  175.     print "Add all_service_commands role ? (0 - no, 1 - yes)\n";
  176.     my $all_service_commands = <>;
  177.     chomp ($all_service_commands);
  178.     print "Add all_host_commands role ? (0 - no, 1 - yes)\n";
  179.     my $all_host_commands = <>;
  180.     chomp ($all_host_commands);
  181.    
  182.     print "Adding roles for user $nagios_user_create\n";
  183.     my $sthUserAuthorizationTable = $dbh->prepare('insert into ninja_user_authorization(user_id, system_information, configuration_information, system_commands, all_services, all_hosts, all_service_commands, all_host_commands)
  184.         VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
  185.     $sthUserAuthorizationTable->bind_param(1, $new_user_id);
  186.     $sthUserAuthorizationTable->bind_param(2, $system_information);
  187.     $sthUserAuthorizationTable->bind_param(3, $configuration_information);
  188.     $sthUserAuthorizationTable->bind_param(4, $system_commands);
  189.     $sthUserAuthorizationTable->bind_param(5, $all_services);
  190.     $sthUserAuthorizationTable->bind_param(6, $all_hosts);
  191.     $sthUserAuthorizationTable->bind_param(7, $all_service_commands);
  192.     $sthUserAuthorizationTable->bind_param(8, $all_host_commands);
  193.     $sthUserAuthorizationTable->execute();
  194. }
  195.  
  196. sub help {
  197.    print "\nAdd User Nagios\n";
  198.    usage();
  199.    print <<EOT;
  200. --help
  201.    print this help message
  202. --u --nagios_user=NAGIOS_USER
  203.    new Nagios username e.g. test_user (without domain!)
  204. --dbu --username_db=DATABASE_USERNAME
  205.    database username
  206. --dbp --password_db=DATABASE_PASSWORD
  207.    database password
  208. --dbd --database=DATABASE_NAME
  209.    database name
  210. Additional info : run script as a root!
  211. EOT
  212. }
  213.  
  214. sub check_input {
  215.     Getopt::Long::Configure ("bundling");
  216.     GetOptions(
  217.         'help'    => \$help,
  218.         'u=s'     => \$new_nagios_user,           'nagios_user:s'    => \$new_nagios_user,
  219.         'dbu=s'   => \$username_db,               'username_db:s'    => \$username_db,
  220.         'dbp=s'   => \$password_db,               'password_db:s'    => \$password_db,
  221.         'dbd=s'   => \$database,                   'database:s'      => \$database
  222.     );
  223.  
  224. if ($help) { help(); exit; }
  225. if (!defined($new_nagios_user))
  226.     { print "Put new username nagios to create! (-h for help)\n"; usage(); exit;}
  227. if (!defined($username_db) || !defined($password_db))
  228.     { print "Put database login or password info! (-h for help)\n"; usage(); exit;}
  229. if (!defined($database))
  230.     { print "Put database name! (-h for help)\n"; usage(); exit;}
  231. }
  232.  
  233. ######### MAIN PROGRAM
  234.  
  235. check_input();
  236.  
  237. ######### Connect to merlin database
  238. print "Checking connection to database $database\n";
  239. connect_db_merlin();
  240.  
  241. ######### Duplicate user field on <YOUR_NAGIOS_SERVER> in /opt/nagios/etc/htpasswd.users
  242. print "Duplicate user entry in file $htpasswd_directory$htpasswd_file\n";
  243. duplicate_htpasswd($new_nagios_user);
  244.  
  245. ######### Adding authorization to file /opt/nagios/etc/cgi.cfg
  246. print "Adding authorizations for new user to file $nagios_cgi_directory$nagios_cgi_cfg_file\n";
  247. duplicate_st1monms2_cgi($new_nagios_user);
  248.  
  249. ######### Restarting nagios
  250. print "Restarting nagios... please wait\n";
  251. restart_nagios();
  252.  
  253. ######### Adding records to database
  254. print "Adding records to database\n";
  255. merlin_db_operations($new_nagios_user);
  256.  
  257. ######### Disconnect from database
  258. print "Disconnect from database\n";
  259. $dbh->disconnect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement