Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use JSON::RPC::Legacy::Client;
  5. use XML::LibXML;
  6.  
  7. my $url      = 'http://<zabbix URL>/zabbix/api_jsonrpc.php'; # URL to Zabbix
  8. my $userid   = "<api user>";                                 # Zabbix API User ID
  9. my $password = "<api password>";                             # Zabbix API User Password
  10. my $destDir  = "./templates";
  11.  
  12. my $client = new JSON::RPC::Legacy::Client;
  13.  
  14. my $authID = authenticate();
  15. my $response = listTemplates();
  16.  
  17. # Check if response was successful
  18. die "template.get failed\n" unless $response->content->{result};
  19.  
  20. unless (-d "$destDir") {
  21.   mkdir "$destDir" or die "$!";
  22. }
  23.  
  24. foreach my $host (@{$response->content->{result}}) {
  25.   # print "Template: ".$host->{host}."\n";
  26.   # print "TemplateID: ".$host->{templateid}."\n";
  27.   getTemplates($host->{templateid}, $host->{host});
  28. }
  29.  
  30. exit(0);
  31.  
  32. # Authenticate
  33. sub authenticate{
  34.   my $json = {
  35.     jsonrpc => "2.0",
  36.     method => "user.login",
  37.     params => {
  38.       user => "$userid",
  39.       password => "$password"
  40.     },
  41.     id => 1
  42.   };
  43.  
  44.   $response = $client->call($url, $json);
  45.     die "Authentication failed\n" unless $response->content->{'result'};
  46.  
  47.   return $response->content->{'result'};
  48. }
  49.  
  50. # Get list of all Templates
  51. sub listTemplates{
  52.   my $json = {
  53.     jsonrpc => '2.0',
  54.     method  => 'template.get',
  55.     params  =>  {
  56.       output => "extend",
  57.      # filter => {
  58.      #   host => "t.app.strongmail"
  59.      #   }
  60.     },
  61.     id   => 2,
  62.     auth => "$authID",
  63.   };
  64.   return $client->call($url, $json);
  65. }
  66.  
  67. # Get Templates and save to individual XML files
  68. sub getTemplates{
  69.   my $templateid = shift;
  70.   my $templatename = shift;
  71.  
  72.   print "TemplateID: $templateid\n";
  73.   print "templatename: $templatename\n";
  74.  
  75.   my $json = {
  76.     jsonrpc => '2.0',
  77.     method  => 'configuration.export',
  78.     params  =>  {
  79.         options => {
  80.             templates => ["$templateid"]
  81.         },
  82.         format => "xml"
  83.     },
  84.     id   => 7,
  85.     auth => "$authID",
  86.   };
  87.  
  88.   $response = $client->call($url, $json);
  89.     die "Authentication failed\n" unless $response->content->{'result'};
  90.  
  91.   my $doc = XML::LibXML->load_xml(string => $response->content->{'result'}, { no_blanks => 1 });
  92.   my $output = $doc->toString(1);
  93.  
  94.   open (MYFILE, ">", "$destDir/$templatename.xml") or die "$!";
  95.   print MYFILE $output;
  96.   close (MYFILE);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement