#!/usr/bin/perl
# op5 Monitor HTTP API example using Perl.
# Creates a host and adds a service.
# Author: Misiu Pajor (mpajor@op5.com), Joel Rangsmo (jrangsmo@op5.com)

# The intentions of this script is to
# demo how the op5 Monitor HTTP API works
# in terms of adding a hard-coded host
# and adding a service to it.

use LWP::UserAgent;
use JSON;
use warnings;
use strict;

my $op5_username = "monitor"; # Username for authentication
my $op5_password = "monitor"; # Password for authentication
my $op5_server = "127.0.0.1"; # Host address for op5 Monitor server

my $ua = LWP::UserAgent->new;
my $json = new JSON;

# Uncomment this line if you use LWP 6.0 or later
# $ua->ssl_opts( verify_hostname => 0 );  # Disables certificate verification for HTTPS - not recommended in production!

# For full list of configuration options see: https://<your.monitor.host>/api/help/config/host
my %host = (    'host_name' => 'api-example-host-1', # hostname of the host you are adding (eg. "mailserver-1")
                'address' => '127.0.0.1', # Host address of api-example-host-1
                'alias' => 'Host used in API examples', # alias of the host to add (eg. "Primary mail server")
                'template' => 'default-host-template' ); # The template sets the remaining host configuration values

# For full list of configuration options see: https://<your.monitor.host>/api/help/config/service
my %service = ( 'host_name' => 'api-example-host-1', # The newly created host in op5 Monitor
                'check_command' => 'check_http', # This tells Monitor which command should be run to determine status of the service
                'service_description' => 'Web server', # Name of service
                'template' => 'default-service' ); # The template sets the remaining service configuration values

my $json_host = $json->encode(\%host);
my $json_service = $json->encode(\%service);


# Let's add the host to op5 Monitor
print "\n\nCreating example host in Monitor...\n\n";
my $req = HTTP::Request->new(POST => "https://$op5_server/api/config/host", undef, $json_host);
$req->header('Content-Type' => 'application/json');
$req->authorization_basic("$op5_username", "$op5_password");
print $ua->request($req)->as_string; # print the response from the API (DEBUG PURPOSES)

# Now, since the host is added, we can add the ping service to it.
# you can repeat the below if you want to add more services, but make sure to change the services hash
# before doing so.
print "\n\nCreating example service in Monitor...\n\n";
$req = HTTP::Request->new(POST => "https://$op5_server/api/config/service", undef, $json_service);
$req->header('Content-Type' => 'application/json');
$req->authorization_basic("$op5_username", "$op5_password");
print $ua->request($req)->as_string; # print the response from the API (DEBUG PURPOSES)

# Save changes in Monitor (instead of having to do this manually,
# we automate the process by saving our changes via the API)
print "\n\nSaving configuration changes in Monitor...\n\n";
$req = HTTP::Request->new(POST => "https://$op5_server/api/config/change", undef, undef);
$req->header('Content-Type' => 'application/json');
$req->authorization_basic("$op5_username", "$op5_password");
print $ua->request($req)->as_string; # print the response from the API (DEBUG PURPOSES)
