Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.33 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use JSON;
  4. use List::Util;
  5. use Zabbix::Tiny;
  6. use Data::Dumper;
  7. use zabbixhost;
  8.  
  9. my @url = ("http://zabbix/api_jsonrpc.php", 'http://192.168.66.114/api_jsonrpc.php');
  10. my $oldZabbix;
  11. my $newZabbix;
  12. my @hostAttributes = zabbixhost->giveAttributes;
  13. my $tiniesJSON;
  14. my $hostilon;
  15. my $username = "xx";
  16. my $password = "xx";
  17. my %functions = (
  18.     CopyHost => \&getHostAttributesFromServer,
  19.     HostToServer => \&createHostsOnServer,
  20.     Login => \&loginInput,
  21.     Exit => sub {return 0},
  22.     );
  23.  
  24. &loginInput;
  25. while(&menue){};
  26.  
  27.  
  28. sub menue{
  29.     print "Choose wisely\n\n";
  30.     print "$_\n" for keys %functions;
  31.     my $task = <STDIN>;
  32.     chomp($task);
  33.     if(exists $functions{$task}){
  34.         print "\n\n";
  35.         $functions{$task}->();
  36.     }else{
  37.         print "Fehlerhafte eingabe try again\n\n";
  38.     }
  39.     return 1;
  40. }
  41.  
  42. sub createHostsOnServer{
  43.     getHostAttributesFromServer();
  44.  
  45.     my @host;
  46.     my @arguments;
  47.     for ( @{$tiniesJSON} ) {
  48.         #print Dumper($_)."\n";
  49.         push @host,$_;
  50.     }
  51.     print Dumper(@arguments);
  52.  
  53.     $newZabbix->do(
  54.         'host.create',
  55.         output => [@host]
  56.     );
  57. }
  58.  
  59. sub getHostAttributesFromServer{
  60.     chomp (@hostAttributes);
  61.     $tiniesJSON = $oldZabbix->do(
  62.         'host.get',  # First argument is the Zabbix API method
  63.         output    => [@hostAttributes],  # Remaining parameters to 'do' are the params for the oldZabbix method.
  64.         monitored => 1,
  65.         limit => 2
  66.     );
  67.     return 1;
  68. }
  69.  
  70. sub loginInput{
  71.     foreach my $server(@url){
  72.         print "Login Process for $server\n";
  73.         print "Username: ";
  74.         chomp( $username=<STDIN>);
  75.         print "Password: ";
  76.         chomp( $password=<STDIN>);
  77.  
  78.         login($username,$password,$server);
  79.     }
  80. }
  81.  
  82. sub login{
  83.     my $server;
  84.     if(@_ == 3){
  85.         $username=shift (@_);
  86.         $password=shift (@_);
  87.         $server=shift(@_);
  88.     } else {
  89.         chomp($username);
  90.         chomp($password);
  91.     }
  92.     eval {
  93.         if($server eq $url[0]){
  94.         $oldZabbix = Zabbix::Tiny->new(
  95.             server   => $server,
  96.             password => $password,
  97.             user     => $username
  98.         );
  99.         }else{
  100.             $newZabbix = Zabbix::Tiny->new(
  101.             server   => $server,
  102.             password => $password,
  103.             user     => $username
  104.         );
  105.         }
  106.         if ($@) {
  107.             print "\n\nLogin or URL are wrong, program is exiting now!\n\n";
  108.             return 0;
  109.         } else {
  110.             print "\n\nLogin sucessfully performed on $server\n\n";
  111.             return 1;
  112.         }
  113.     };
  114. }
  115.  
  116. sub checkForDuplicates {
  117.  
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement