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:

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:

Headers:

Parameters:

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:

Parameters:

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:

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:

Parameters:

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"
["Opsview"] ["API", "Technical Reference"]

Was this topic helpful?