Config - Host Templates
Object type: hosttemplate
Request URL: /rest/config/hosttemplate
Example GET
{
"object" : {
"hosts" : [
{
"ref" : "/rest/config/host/7",
"name" : "cisco"
}
],
"name" : "Cisco Mgt",
"description" : "Cisco device Management URLs",
"id" : "3",
"has_icon" : 0,
"servicechecks" : [
{
"ref" : "/rest/config/servicecheck/45",
"name" : "Check Loadavg",
"exception":"-w 5,5,5 -c 9,9,9",
"timed_exception":null,
},
{
"ref" : "/rest/config/servicecheck/4",
"name" : "DNS",
"exception":"--other --args",
"timed_exception":null,
},
{
"ref" : "/rest/config/servicecheck/6",
"name" : "HTTP",
"timed_exception":{
"timeperiod" : { "name" : "workhours" },
"args" : "--url=%URL% -w 15 -c 20",
},
"exception":null,
],
"managementurls" : [
{
"url" : "ssh://$HOSTADDRESS$",
"name" : "SSH",
"id" : "4"
},
{
"url" : "telnet://$HOSTADDRESS$",
"name" : "Telnet",
"id" : "5"
}
],
"uncommitted" : "1"
}
}
Notes on attributes:
remove_hostservicechecks
— see the Host Templates configuration section for information about this attribute.has_icon
— if set to 0, then there is no icon available. Otherwise will be set to epoch seconds as the time that the icon was updated. Icons can be found at /images/hticons/HTID/100×100.png, where HTID is the host template id number. There are also icons in 40×40.png and 20×20.png
Host Template - Upload
Host template is staged, parsed and checked for conflicts to see if an import would be successful.
Requires: ADMINACCESS.
URL: /rest/config/hosttemplate/upload
Method:
POST
- upload new opspack.GET
,PUT
,DELETE
- unimplemented.
Headers:
- Content-Type must be set to a form upload format, as the file will be uploaded
Parameters:
filename
- file name to upload. Filename must not contain “..” or “/” or spaces. This will return status 200 unless there is an exceptional failure. Will log to audit log when submitted.
Returns for a failure with status 200:
{
success: false,
error: "Opspack contains ....", // If success is false, will return an internationalised string for display
}
Returns for a success:
{
success: true,
stdout: ['text from opspack','next line'], // Strings from the opspack test import process
exists: true, // Returns true if there is any conflict in the opspack for importing. Details in the stdout
}
Host Template - Import
Requires: ADMINACCESS.
Needs changelog if enabled.
URL: /rest/config/hosttemplate/import
Method:
POST
— import opspack.GET
,PUT
,DELETE
— unimplemented.
Parameters:
filename
— opspack to import from the prior upload.overwrite
— default 0. If 1, force import of opspack.changelog
— required if changelog is enabled. This will log to audit log when submitted.
Returns:
{
success: true,
return_code: 0, // Return code from the opspack import process
stdout: [], // Array of strings from opspack import output
stderr: [], // Array of strings from opspack import errors
}
Example import script
This example script in perl, import_opspack_via_rest.pl
, shows how to code the upload & import of an Opspack.
#!/usr/bin/perl
#
# SYNTAX:
# import_opspack_via_rest <opspack_file>
#
# DESCRIPTION:
# Quick script to import Opspack via Opsview's REST API
use warnings;
use strict;
use WWW::Mechanize;
use JSON;
my $opspack = shift @ARGV
|| die "Must specify an opspack file to upload and import";
my $url_prefix = "http://localhost";
my $username = "admin";
my $password = "initial";
my $upload_filename = "test.opspack";
my $ua = WWW::Mechanize->new;
my $res;
$ua->add_header(
"Accept" => "application/json"
);
$ua->post(
"$url_prefix/rest/login",
Content => [
username => $username,
password => $password,
]
);
$res = decode_json( $ua->content );
$ua->add_header( "X-Opsview-Username", $username );
$ua->add_header( "X-Opsview-Token", $res->{token} );
$ua->post(
"$url_prefix/rest/config/hosttemplate/upload",
"Content-Type" => "form-data",
"Content" => [
filename => [
$opspack,
$upload_filename,
"Content-type" => "application/octet-stream",
],
],
);
if ( $ua->status != 200 ) {
die "Unexpected failure";
}
$res = decode_json( $ua->content );
if ( !$res->{success} ) {
die "Failure uploading opspack";
}
if ( @{ $res->{stdout} } ) {
print "Uploaded opspack information\n";
print join( "", @{ $res->{stdout} } );
print "\n";
}
my $force = 0;
if ( $res->{exists} ) {
print "Opspack already exists. Press enter to force upload: ";
$_ = <>;
$force = 1;
}
$ua->add_header(
"Accept" => "application/json"
);
$ua->post(
"$url_prefix/rest/config/hosttemplate/import",
"Content" => [
filename => $upload_filename,
overwrite => $force,
],
);
if ( $ua->status != 200 ) {
die "Unexpected failure";
}
$res = decode_json( $ua->content );
if ( !$res->{success} ) {
die "Failure uploading opspack";
}
if ( @{ $res->{stdout} } ) {
print "IMPORTED OPSPACK INFORMATION\n";
print join( "", @{ $res->{stdout} } );
print "\n";
}
Host Template - Export Opspack
Prepares an opspack for export. Take the generated filename for retrieving in a subsequent call.
Requires: ADMINACCESS.
URL: /rest/config/hosttemplate/ID/export
Method:
POST
— starts an opspack export.DELETE
— unimplemented.
Returns:
{
success: true,
filename: "FILENAME.opspack" // Will be a generated temporary name
}
Host Template - Retrieve Opspack
Retrieves a previously exported opspack. When the file is downloaded, the file will be removed.
No specific access required.
URL: /rest/exportedFile
Method:
GET
— retrieves an exported opspack.POST
,PUT
,DELETE
— unimplemented.
Parameters:
filename
— opspack to retrieve. Use the temporary filename from the Export Opspack call. This returns the contents of the file, with a content type of “application/octet-stream”.
Example export process
$ opsview_rest --username=admin --password=initial --pretty --data-format=json POST "config/hosttemplate/1/export"
{
"filename" : "opsview-component-agent:tempcman1ESWWKpFnxx3.opspack",
"success" : true
}
$ wget --header="Accept: application/json" -O saved.opspack "http://localhost/rest/exportedFile?filename=opsview-component-agent:tempcman1ESWWKpFnxx3.opspack"