Scripts for creating a ticket from the command line
This page contains example scripts, in various
languages, to create a ticket in iTop from the command line. The
scripts use the REST/JSON API of iTop to create a
ticket on a remote iTop server (the script must have access to iTop
through http/https). The scripts are intended for use with Nagios,
since they expect 4 parameters: host
,
service
, service_status
and
service_state_type
, but the only meaningful parameter
is the host
and it is quite easy to adjust the scripts
to your specific needs.
Pick the script in your favorite scripting language and don't hesitate to tailor it.
Using PHP
This section provides an example script in the PHP scripting language to create a ticket in a (remote) iTop using the REST/JSON webservices. For the simplicity of this example, only a minimal validation of the parameters and return status is performed.
Requirements: PHP with the cURL
module.
Limitation: the “host” value in Nagios must correspond to the name of a unique FunctionalCI in iTop.
The following PHP script creates a UserRequest ticket (you can change the configuration to create either an Incident or a Change) and attaches the supplied host (= FunctionalCI) to it. The ticket is created in the same Organization as the host.
- create-ticket.php
-
#!/usr/bin/php <?php // Configuration $ITOP_URL = 'https://demo.combodo.com/simple'; $ITOP_USER = 'admin'; $ITOP_PWD = 'admin'; $TICKET_CLASS = 'UserRequest'; $TITLE = 'Service Down on %1$s'; $DESCRIPTION = 'The Service "%2$s" is down on "%1$s"'; $COMMENT = 'Created from PHP'; if ($argc != 5) { echo "Usage: {$argv[0]} <host> <service> <service_status> <service_state_type>\n"; exit; } $host = $argv[1]; $service = $argv[2]; $service_status = $argv[3]; $service_state_type = $argv[4]; $url = $ITOP_URL.'/webservices/rest.php?version=1.0'; if (($service_status != "OK") && ($service_status != "UP") && ($service_state_type == "HARD")) { $payload = array( 'operation' => 'core/create', 'class' => $TICKET_CLASS, 'fields' => array( 'org_id' => sprintf('SELECT Organization AS O JOIN FunctionalCI AS CI ON CI.org_id = O.id WHERE CI.name="%1$s"', $host), 'title' => sprintf($TITLE, $host, $service), 'description' => sprintf($DESCRIPTION, $host, $service), 'functionalcis_list' => array( array('functionalci_id' => sprintf("SELECT FunctionalCI WHERE name='%s'", $host), 'impact_code' => 'manual'), ), ), 'comment' => $COMMENT, 'output_fields' => 'id', ); $data = array( 'auth_user' => $ITOP_USER, 'auth_pwd' => $ITOP_PWD, 'json_data' => json_encode($payload) ); $options = array( CURLOPT_POST => count($data), CURLOPT_POSTFIELDS => http_build_query($data), // Various options... CURLOPT_RETURNTRANSFER => true, // return the content of the request CURLOPT_HEADER => false, // don't return the headers in the output CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response // Disabling SSL verification CURLOPT_SSL_VERIFYPEER => false, // Disable SSL certificate validation CURLOPT_SSL_VERIFYHOST => false, // Disable host vs certificate validation ); $handle = curl_init($url); curl_setopt_array($handle, $options); $response = curl_exec($handle); $errno = curl_errno($handle); $error_message = curl_error($handle); curl_close($handle); if ($errno !== 0) { echo "Problem opening URL: $url, $error_message\n"; exit; } $decoded_response = json_decode($response, true); if ($decoded_response === false) { echo "Error: ".print_r($response, true)."\n"; } else if ($decoded_response['code'] != 0) { echo $decoded_response['message']."\n"; } else { echo "Ticket created.\n"; } } else { echo "Service state type !='HARD', doing nothing.\n"; }
Usage
create-ticket.php <host> <service> <service_status> <service_state_type>
Configuration
Edit the following lines (at the beginning of the script) to adjust the script to your environment:
// Configuration $ITOP_URL = 'https://demo.combodo.com/simple'; $ITOP_USER = 'admin'; $ITOP_PWD = 'admin'; $TICKET_CLASS = 'UserRequest'; $TITLE = 'Service Down on %1$s'; $DESCRIPTION = 'The Service "%2$s" is down on "%1$s"'; $COMMENT = 'Created from PHP'
Troubleshooting
You can test the ticket creation by running the script manually.
For example, if a server called Server1
exists in your
iTop, you can run the following command to create a ticket:
create-ticket.php "Server1" "Manual Test" "DOWN" "HARD"
Using Perl
This section provides an example script in the Perl scripting language to create a ticket in a (remote) iTop using the REST/JSON webservices. For the simplicity of this example, only a minimal validation of the parameters and return status is performed.
Requirements: Perl with the LWP
and JSON
modules.
Limitation: the “host” value in Nagios must correspond to the name of a unique FunctionalCI in iTop.
The following Perl script creates a UserRequest ticket (you can change the configuration to create either an Incident or a Change) and attaches the supplied host (= FunctionalCI) to it. The ticket is created in the same Organization as the host.
- create-ticket.pl
-
#!/usr/bin/perl use strict; use warnings; use LWP 5.64; use JSON; # Default values my $ITOP_URL = 'https://demo.combodo.com/simple'; my $ITOP_LOGIN = "admin"; my $ITOP_PWD = "admin"; my $TICKET_CLASS = 'UserRequest'; my $DEFAULT_DESCRIPTION = 'Service "%1$s" is down on host "%2$s"'; my $DEFAULT_TITLE = 'Service down on "%1$s"'; my $COMMENT = "Created from $0"; # Parameters checking my ($host, $service, $service_status, $service_state_type) = @ARGV; if (not defined $host) { die "Parameter 1: 'host' needed.\n"; } if (not defined $service) { die "Parameter 2: 'service' needed.\n"; } if (not defined $service_status) { die "Parameter 3: 'service status' needed.\n"; } if (not defined $service_state_type) { die "Parameter 4: 'service state type' needed.\n"; } if ( ($service_status ne "OK" ) && ( $service_status ne "UP" ) && ( $service_state_type eq "HARD" )) { my $url = "$ITOP_URL/webservices/rest.php?version=1.0"; my $browser = LWP::UserAgent->new; $browser->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00); my %ci_link = ( 'functionalci_id' => "SELECT FunctionalCI WHERE name='$host'", 'impact_code' => 'manual'); my %fields = ( 'org_id' => "SELECT Organization AS O JOIN FunctionalCI AS CI ON CI.org_id=O.id WHERE CI.name='$host'", 'title' => sprintf($DEFAULT_TITLE, $host), 'description' => sprintf($DEFAULT_DESCRIPTION, $service, $host), 'functionalcis_list' => [ \%ci_link ], ); my %data = ( 'operation' => 'core/create', 'class' => $TICKET_CLASS, 'comment' => $COMMENT, 'output_fields' => 'id', 'fields' => \%fields, ); my $response = $browser->post($url, [ 'auth_user' => $ITOP_LOGIN, 'auth_pwd' => $ITOP_PWD, 'json_data' => encode_json \%data, ]); if ($response->is_success) { my $output = decode_json( $response->decoded_content); # or whatever if ($output->{'code'} != 0) { print $output->{'message'}."\n"; } else { print "Ticket created.\n"; } } else { die $response->status_line; } } else { print "Service state type !='HARD', doing nothing.\n"; }
Usage
create-ticket.pl <host> <service> <service_status> <service_state_type>
Configuration
Edit the following lines (at the beginning of the script) to adjust the script to your environment:
# Default values my $ITOP_URL = 'https://demo.combodo.com/simple'; my $ITOP_LOGIN = "admin"; my $ITOP_PWD = "admin"; my $TICKET_CLASS = 'UserRequest'; my $DEFAULT_DESCRIPTION = 'Service "%1$s" is down on host "%2$s"'; my $DEFAULT_TITLE = 'Service down on "%1$s"'; my $COMMENT = "Created from $0";
Troubleshooting
You can test the ticket creation by running the script manually.
For example, if a server called Server1
exists in your
iTop, you can run the following command to create a ticket:
create-ticket.pl "Server1" "Manual Test" "DOWN" "HARD"
Using Python
This section provides an example script in the python scripting language to create a ticket in a (remote) iTop using the REST/JSON webservices. For the simplicity of this example, only a minimal validation of the parameters and return status is performed.
Requirements: python with the
requests
, json
and (obviously)
sys
packages.
Limitation: the “host” value in Nagios must correspond to the name of a unique FunctionalCI in iTop.
The following python script creates a UserRequest ticket (you can change the configuration to create either an Incident or a Change) and attaches the supplied host (= FunctionalCI) to it. The ticket is created in the same Organization as the host.
- create-ticket.py
-
#!/usr/bin/python import requests import json import sys ITOP_URL = 'https://demo.combodo.com/simple' ITOP_USER = 'admin' ITOP_PWD = 'admin' TICKET_CLASS = 'UserRequest' TITLE = 'Service down on %(host)s' DESCRIPTION = 'The service %(service)s is down on %(host)s' COMMENT = 'Created from Python' if len(sys.argv) != 5: print "Usage: "+sys.argv[0]+" host service service_status service_state_type\n" sys.exit() else: print str(sys.argv) host = sys.argv[1] service = sys.argv[2] service_status = sys.argv[3] service_state_type = sys.argv[4] if (service_status != "OK") and (service_status != "UP") and (service_state_type == "HARD" ): json_data = { 'operation': 'core/create', 'class': TICKET_CLASS, 'fields': { 'title': TITLE % {'host': host }, 'description': DESCRIPTION % {'host': host, 'service': service }, 'org_id': 'SELECT Organization AS O JOIN FunctionalCI AS CI ON CI.org_id = O.id WHERE CI.name="%(host)s"' % {'host': host}, 'functionalcis_list': [ { 'functionalci_id': "SELECT FunctionalCI WHERE name='%(host)s'" % {'host': host}, 'impact_code': 'manual', }], }, 'comment': COMMENT, 'output_fields': 'id', } encoded_data = json.dumps(json_data) r = requests.post(ITOP_URL+'/webservices/rest.php?version=1.0', verify=False, data={'auth_user': ITOP_USER , 'auth_pwd': ITOP_PWD , 'json_data': encoded_data}) result = json.loads(r.text); if result['code'] == 0: print "Ticket created.\n" else: print result['message']+"\n" else: print "Service state type !='HARD', doing nothing.\n"
Usage
create-ticket.py <host> <service> <service_status> <service_state_type>
Configuration
Edit the following lines (at the beginning of the script) to adjust the script to your environment:
ITOP_URL = 'https://demo.combodo.com/simple' ITOP_USER = 'admin' ITOP_PWD = 'admin' TICKET_CLASS = 'UserRequest' TITLE = 'Service down on %(host)s' DESCRIPTION = 'The service %(service)s is down on %(host)s' COMMENT = 'Created from Python'
Troubleshooting
You can test the ticket creation by running the script manually.
For example, if a server called Server1
exists in your
iTop, you can run the following command to create a ticket:
create-ticket.py "Server1" "Manual Test" "DOWN" "HARD"
Using bash and wget
This section provides an example script in the bash scripting language to create a ticket in a (remote) iTop using the REST/JSON webservices. For the simplicity of this example, only a minimal validation of the parameters and return status is performed.
Requirements: bash and wget.
Limitation: the “host” value in Nagios must correspond to the name of a unique FunctionalCI in iTop.
The following bash script creates a UserRequest ticket (you can change the configuration to create either an Incident or a Change) and attaches the supplied CI to it. The ticket is created in the same Organization as the CI.
- create-ticket.bash
-
#!/bin/bash ################################################################################## # # # Example script for creating a UserRequest ticket via the REST/JSON webservices # # # ################################################################################## # iTop location and credentials, change them to suit your iTop installation ITOP_URL="https://demo.combodo.com/simple" ITOP_USER="admin" ITOP_PWD="admin" # Parameters checking, see below for default values if [ "$1" == "" ]; then echo "Missing parameter 1: host" exit -1 else HOST="$1" fi if [ "$2" == "" ]; then echo "Missing parameter 2: Service" exit -1 else SERVICE="$2" fi if [ "$3" == "" ]; then echo "Missing parameter 3: Service Status" exit -1 else SERVICE_STATUS="$3" fi if [ "$4" == "" ]; then echo "Missing parameter 4: Service State Type" exit -1 else SERVICE_STATUS_TYPE="$4" fi # Default values, adapt them to your configuration TICKET_CLASS="UserRequest" ORGANIZATION="SELECT Organization JOIN FunctionalCI AS CI ON CI.org_id=Organization.id WHERE CI.name='"${HOST}"'" TITLE="Service Down on $1" DESCRIPTION="The service $SERVICE is in state $SERVICE_STATUS on $HOST" # Let's create the ticket via the REST/JSON API if [[ ( "$SERVICE_STATUS" != "OK" ) && ( "$SERVICE_STATUS" != "UP" ) && ( "$SERVICE_STATUS_TYPE" == "HARD" ) ]]; then CIS_LIST='[{"functionalci_id":"SELECT FunctionalCI WHERE name=\"'"$1"'\"", "impact_code": "manual"}]' JSON_DATA='{"operation":"core/create", "class":"'"${TICKET_CLASS}"'", "fields": {"functionalcis_list":'"${CIS_LIST}"', "org_id":"'"${ORGANIZATION}"'", "title":"'"$TITLE"'", "description":"'"$DESCRIPTION"'"}, "comment": "Created by the Monitoring", "output_fields": "id"}' RESULT=`wget -q --post-data='auth_user='"${ITOP_USER}"'&auth_pwd='"${ITOP_PWD}"'&json_data='"${JSON_DATA}" --no-check-certificate -O - "${ITOP_URL}/webservices/rest.php?version=1.0"` PATTERN='"key":"([0-9])+"' if [[ $RESULT =~ $PATTERN ]]; then echo "Ticket created successfully" else echo "ERROR: failed to create ticket" echo $RESULT fi else echo "Service State Type != HARD, doing nothing" fi
Usage
create-ticket.bash <host> <service> <service_status> <service_state_type>
Configuration
Change the 3 lines at the top of the script to adjust to your environment
ITOP_URL="https://demo.combodo.com/simple" ITOP_USER="admin" ITOP_PWD="admin"
Then change the default values to your taste:
# Default values, adapt them to your configuration TICKET_CLASS="UserRequest" ORGANIZATION="SELECT Organization JOIN FunctionalCI AS CI ON CI.org_id=Organization.id WHERE CI.name='"${HOST}"'" TITLE="Service Down on $1" DESCRIPTION="The service $SERVICE is in state $SERVICE_STATUS on $HOST"
Troubleshooting
You can test the ticket creation by running the script manually.
For example, if a server called Server1
exists in your
iTop, you can run the following command to create a ticket:
create-ticket.bash "Server1" "Manual Test" "DOWN" "HARD"
Using bash and curl
This section provides an example script in the bash scripting language to update a ticket in a (remote) iTop using the REST/JSON webservices.
Copy paste this command as an example
curl -X POST -F 'version=1.3' -F 'auth_user=admin' -F 'auth_pwd=admin' http://localhost/itop/webservices/rest.php --form-string 'json_data={"operation":"core/create","comment":"Synchronization from blah...", "class":"UserRequest","output_fields":"id, friendlyname", "fields":{"org_id":"SELECT Organization WHERE id=3", "caller_id":{"name":"monet","first_name":"claude"},"title":"issue blah","description":"something happened"}}' |