Skip to content

Commit

Permalink
added check_netscaler.pl
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Dec 17, 2015
1 parent 041d078 commit 60e36fb
Show file tree
Hide file tree
Showing 3 changed files with 732 additions and 3 deletions.
6 changes: 3 additions & 3 deletions LICENSE → LICENSE-2.0.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down Expand Up @@ -178,15 +179,15 @@
APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -199,4 +200,3 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

306 changes: 306 additions & 0 deletions Nitro.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
=begin comment
* Copyright (c) 2008-2015 Citrix Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
=end comment
=cut

package Nitro;

use strict;
use LWP;
use Carp;
use JSON;
use URI::Escape;

# Login method : Used to login to netscaler and get session
# Arguments : ipaddress, username, password (of netscaler)
sub _login {

my ($ipaddress,$username,$password) = @_ ;

if (!$ipaddress || $ipaddress eq "") {
Carp::confess "Error : IP Address should not be null";
}
if (!$username || $username eq "") {
Carp::confess "Error : Username should not be null";
}
if (!$password || $password eq "") {
Carp::confess "Error : Password should not be null";
}
my $obj = undef;
$obj->{username} = $username;
$obj->{password} = $password;
my $payload = JSON->new->allow_blessed->convert_blessed->encode($obj);
$payload = '{"login" :'.$payload."}";

my $url = "http://$ipaddress/nitro/v1/config/login";
my $contenttype = "application/vnd.com.citrix.netscaler.login+json";

my $nitro_useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new( POST => $url );
$request->header( 'Content-Type', $contenttype );
$request->content($payload);

my $response = $nitro_useragent->request($request);
my $session = undef;
if (HTTP::Status::is_error($response->code)) {
$session = JSON->new->allow_blessed->convert_blessed->decode($response->content);
} else {
my $cookie = $response->header('Set-Cookie');
if ($cookie && $cookie =~ /NITRO_AUTH_TOKEN=(.*);/) {
$session->{sessionid} = uri_unescape($1);
}
$session->{errorcode} = 0;
$session->{message} = "Done";
}
$session->{ns} = $ipaddress;
$session->{username} = $username;
$session->{password} = $password;
return $session;
}

# POST method : Used to clear, enable, add, unset, bind, import, export and save the configuration
# Arguments : session, objecttype, object, operation
sub _post {

my ($session, $objecttype, $object, $operation) = @_ ;

if (!$session || $session eq "") {
Carp::confess "Error : Session should not be null";
}
if (!($session->{sessionid})) {
Carp::confess "Error : Not logged in";
}
if (!$objecttype || $objecttype eq "") {
Carp::confess "Error : Object type should not be null";
}
if (!$object || $object eq "") {
Carp::confess "Error : Object should not be null";
}

my $payload = JSON->new->allow_blessed->convert_blessed->encode($object);
$payload = '{"'.$objecttype.'" :'.$payload."}";

my $url = "http://$session->{ns}/nitro/v1/config/".$objecttype;
if ($operation && $operation ne "add") {
$url = $url. "?action=".$operation;
}
my $contenttype = "application/vnd.com.citrix.netscaler.".$objecttype."+json";

my $nitro_useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new(POST => $url);
$request->header('Content-Type', $contenttype);
$request->header('Set-Cookie', "NITRO_AUTH_TOKEN=".$session->{sessionid});
$request->content($payload);

my $response = $nitro_useragent->request($request);
if (HTTP::Status::is_error($response->code)) {
$response = JSON->new->allow_blessed->convert_blessed->decode($response->content);
} else {
$response->{errorcode} = 0;
$response->{message} = "Done";
}
return $response;
}

# GET method : Used to get the details of configuration
# Arguments : session, objecttype, objectname, options
sub _get {

my ($session, $objecttype, $objectname, $options) = @_ ;

if (!$session || $session eq "") {
Carp::confess "Error : Session should not be null";
}
if (!($session->{sessionid})) {
Carp::confess "Error : Not logged in";
}
if (!$objecttype || $objecttype eq "") {
Carp::confess "Error : Object type should not be null";
}

my $url = "http://$session->{ns}/nitro/v1/config/".$objecttype;
if ($objectname && $objectname ne "") {
$url = $url."/".uri_escape(uri_escape($objectname));
}
if ($options && $options ne "") {
$url = $url."?".$options;
}
my $contenttype = "application/vnd.com.citrix.netscaler.".$objecttype."+json";

my $nitro_useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new(GET => $url);
$request->header('Content-Type', $contenttype);
$request->header('Set-Cookie', "NITRO_AUTH_TOKEN=".$session->{sessionid});

my $response = $nitro_useragent->request($request);
$response = JSON->new->allow_blessed->convert_blessed->decode($response->content);
return $response;
}

# Get stats method : Used to get the stats of the configuration
# Arguments : session, objecttype, objectname
sub _get_stats {

my ($session, $objecttype, $objectname) = @_ ;

if (!$session || $session eq "") {
Carp::confess "Error : Session should not be null";
}
if (!($session->{sessionid})) {
Carp::confess "Error : Not logged in";
}
if (!$objecttype || $objecttype eq "") {
Carp::confess "Error : Object type should not be null";
}

my $url = "http://$session->{ns}/nitro/v1/stat/".$objecttype;
if ($objectname && $objectname ne "") {
$url = $url. "/".uri_escape(uri_escape($objectname));
}
my $contenttype = "application/vnd.com.citrix.netscaler.".$objecttype."+json";

my $nitro_useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new(GET => $url);
$request->header('Content-Type', $contenttype);
$request->header('Set-Cookie', "NITRO_AUTH_TOKEN=".$session->{sessionid});

my $response = $nitro_useragent->request($request);
$response = JSON->new->allow_blessed->convert_blessed->decode($response->content);
return $response;
}

# PUT method : Used to update the already existing configuration
# Arguments : session, objecttype, object, objectname
sub _put
{
my ($session, $objecttype, $object, $objectname) = @_ ;

if (!$session || $session eq "") {
Carp::confess "Error : Session should not be null";
}
if (!($session->{sessionid})) {
Carp::confess "Error : Not logged in";
}
if (!$objecttype || $objecttype eq "") {
Carp::confess "Error : Object type should not be null";
}
if (!$object || $object eq "") {
Carp::confess "Error : Object should not be null";
}

my $payload = JSON->new->allow_blessed->convert_blessed->encode($object);
$payload = '{"'.$objecttype.'" :'.$payload."}";

my $url = "http://$session->{ns}/nitro/v1/config/".$objecttype. "/".uri_escape(uri_escape($objectname));
my $contenttype = "application/vnd.com.citrix.netscaler.".$objecttype."+json";

my $nitro_useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new(PUT => $url);
$request->header('Content-Type', $contenttype);
$request->header('Set-Cookie', "NITRO_AUTH_TOKEN=".$session->{sessionid});
$request->content($payload);

my $response = $nitro_useragent->request($request);
if (HTTP::Status::is_error($response->code)) {
$response = JSON->new->allow_blessed->convert_blessed->decode($response->content);
} else {
$response->{errorcode} = 0;
$response->{message} = "Done";
}
return $response;
}

# DELETE method : Used to delete, unbind the existing configuration
# Arguments : session, objecttype, object
sub _delete {

my ($session, $objecttype, $object) = @_ ;

if (!$session || $session eq "") {
Carp::confess "Error : Session should not be null";
}
if (!($session->{sessionid})) {
Carp::confess "Error : Not logged in";
}
if (!$objecttype || $objecttype eq "") {
Carp::confess "Error : Object type should not be null";
}
if (!$object || $object eq "") {
Carp::confess "Error : Object should not be null";
}

my $url = "http://$session->{ns}/nitro/v1/config/$objecttype";
if (ref($object) eq 'HASH') {
$url = $url."?args=";
while ((my $key, my $value) = each %{$object}) {
$url = $url.$key.":".uri_escape(uri_escape($value)).",";
}
$url =~ s/,$//;
} else {
$url = $url."/".uri_escape(uri_escape($object));
}
my $contenttype = "application/vnd.com.citrix.netscaler.".$objecttype."+json";

my $nitro_useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new(DELETE => $url);
$request->header('Content-Type', $contenttype);
$request->header('Set-Cookie', "NITRO_AUTH_TOKEN=".$session->{sessionid});

my $response = $nitro_useragent->request($request);
if (HTTP::Status::is_error($response->code)) {
$response = JSON->new->allow_blessed->convert_blessed->decode($response->content);
} else {
$response->{errorcode} = 0;
$response->{message} = "Done";
}
return $response;
}

# Logout method : Used to logout the netscaler
# Arguments : session
sub _logout {

my ($session) = @_ ;

if (!$session || $session eq "") {
Carp::confess "Error : Session should not be null";
}
if (!($session->{sessionid})) {
Carp::confess "Error : Not logged in";
}

my $payload = '{"logout" :{}}';

my $url = "http://$session->{ns}/nitro/v1/config/logout";
my $contenttype = "application/vnd.com.citrix.netscaler.logout+json";

my $nitro_useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new(POST => $url);
$request->header('Content-Type', $contenttype);
$request->header('Set-Cookie', "NITRO_AUTH_TOKEN=".$session->{sessionid});
$request->content($payload);

my $response = $nitro_useragent->request($request);
if (HTTP::Status::is_error($response->code)) {
$response = JSON->new->allow_blessed->convert_blessed->decode($response->content);
} else {
$response->{errorcode} = 0;
$response->{message} = "Done";
}
return $response;
}

1;

Loading

0 comments on commit 60e36fb

Please sign in to comment.