Not sure if this is useful to anyone else but I've implemented Implemented
three new SCGI actions that return JSON. I use it to control my hosts via
some proprietary software that interprets JSON.
- ?action=json_status&host=<HOST>
If host is not specified we return JSON object of the BackupPC
%Status hash. If host equals "server" we return the BackupPC
%Info hash. If host equals a valid backup host, we return
the BackupPC $Status{$host}.
- ?action=add_host&host=<HOST>
Creates a new host config and reloads the server configuration.
- ?action=modify_host&host=<HOST>&ip=<IP_ADDRESS|HOSTNAME>&disable=<0|1>
Modifies host ClientNameAlias and BackupsDisable configuration items.
Could easily be extended to set more attributes.
From 4ffaf87e83dc0bee2c000515dffae3ba369c36a9 Mon Sep 17 00:00:00 2001
From: Nicholas Hall <[email protected]>
Date: Fri, 24 Apr 2015 12:55:23 -0500
Subject: [PATCH] Simple API methods that return JSON
Implemented three new SCGI actions
- ?action=json_status&host=<HOST>
If host is not specified we return JSON object of the BackupPC
%Status hash. If host equals "server" we return the BackupPC
%Info hash. If host equals a valid backup host, we return
the BackupPC $Status{$host}.
- ?action=add_host&host=<HOST>
Creates a new host config and reloads the server configuration.
- ?action=modify_host&host=<HOST>&ip=<IP_ADDRESS|HOSTNAME>&disable=<0|1>
Modifies host ClientNameAlias and BackupsDisable configuration items.
---
bin/BackupPC_Admin_SCGI | 6 +++
lib/BackupPC/CGI/AddHost.pm | 93 ++++++++++++++++++++++++++++++++++++++++++
lib/BackupPC/CGI/JSONStatus.pm | 72 ++++++++++++++++++++++++++++++++
lib/BackupPC/CGI/ModifyHost.pm | 89 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 260 insertions(+)
create mode 100644 lib/BackupPC/CGI/AddHost.pm
create mode 100644 lib/BackupPC/CGI/JSONStatus.pm
create mode 100644 lib/BackupPC/CGI/ModifyHost.pm
diff --git a/bin/BackupPC_Admin_SCGI b/bin/BackupPC_Admin_SCGI
index 2df459e..3566549 100755
--- a/bin/BackupPC_Admin_SCGI
+++ b/bin/BackupPC_Admin_SCGI
@@ -86,6 +86,9 @@ use BackupPC::CGI::StartStopBackup;
use BackupPC::CGI::StopServer;
use BackupPC::CGI::Summary;
use BackupPC::CGI::View;
+use BackupPC::CGI::JSONStatus;
+use BackupPC::CGI::AddHost;
+use BackupPC::CGI::ModifyHost;
my %ActionDispatch = (
"summary" => "Summary",
@@ -112,6 +115,9 @@ my %ActionDispatch = (
"Stop" => "StopServer",
"adminOpts" => "AdminOptions",
"editConfig" => "EditConfig",
+ "json_status" => "JSONStatus",
+ "add_host" => "AddHost",
+ "modify_host" => "ModifyHost",
);
my %ChildPid2Num;
diff --git a/lib/BackupPC/CGI/AddHost.pm b/lib/BackupPC/CGI/AddHost.pm
new file mode 100644
index 0000000..501fb93
--- /dev/null
+++ b/lib/BackupPC/CGI/AddHost.pm
@@ -0,0 +1,93 @@
+#============================================================= -*-perl-*-
+#
+# BackupPC::CGI::AddHost package
+#
+# DESCRIPTION
+#
+# This module implements an interface to add hosts via external call.
+#
+# AUTHOR
+# Nicholas Hall <[email protected]>
+#
+# COPYRIGHT
+# Copyright (C) 2003-2013 Craig Barratt
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#========================================================================
+#
+# Version 4.0.0alpha3, released 1 Dec 2013.
+#
+# See http://backuppc.sourceforge.net.
+#
+#========================================================================
+
+package BackupPC::CGI::AddHost;
+
+use strict;
+use BackupPC::CGI::Lib qw(:all);
+use JSON;
+
+sub action {
+ my %output;
+ my $host = $1 if ( $In{host} =~ /(.*)/ );
+
+ print 'Content-type: application/json', "\r\n\r\n";
+
+ if ($host eq "") {
+ $output{'status'} = JSON::false;
+ $output{'message'} = "Missing paramenter 'host' in request";
+
+ print encode_json \%output;
+ return 0;
+ }
+
+ # Check for dupe
+ GetStatusInfo("hosts");
+ if (defined($Status{$host})) {
+ $output{'status'} = JSON::false;
+ $output{'message'} = "Host already exists";
+
+ print encode_json \%output;
+ return 0;
+ }
+
+ # Add line to hosts file
+ my $file = $bpc->ConfDir() . "/hosts";
+ open(my $host_fh, '>>', $file) or die("Could not open hosts file");
+ print $host_fh "${host}\t0\n";
+ close $host_fh;
+
+ # Write out host config
+ my $file = $bpc->ConfDir() . "/pc/${host}.pl";
+ my $config = <<'EOF';
+$Conf{ClientNameAlias} = 'undefined';
+$Conf{BackupsDisable} = 1;
+EOF
+ open(my $config_fh, '>', $file) or die("Count not open host config file");
+ print $config_fh $config;
+ close $config_fh;
+
+ # Log and reload server configuration
+ $bpc->ServerMesg("log JSON interface added host ${host}");
+ ServerConnect();
+ $bpc->ServerMesg("log JSON interface requested server configuration reload");
+ $bpc->ServerMesg("server reload");
+
+ $output{'status'} = JSON::true;
+ $output{'message'} = "Host was added successfully";
+ print encode_json \%output;
+}
+
+1;
diff --git a/lib/BackupPC/CGI/JSONStatus.pm b/lib/BackupPC/CGI/JSONStatus.pm
new file mode 100644
index 0000000..0d1644c
--- /dev/null
+++ b/lib/BackupPC/CGI/JSONStatus.pm
@@ -0,0 +1,72 @@
+#=============================================================
+#
+# BackupPC::CGI::JSONStatus package
+#
+# DESCRIPTION
+#
+# This module exposes host and server status in JSON.
+#
+# AUTHOR
+# Nicholas Hall <[email protected]>
+#
+# COPYRIGHT
+# Copyright (C) 2005-2013 Rich Duzenbury and Craig Barratt
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#========================================================================
+#
+# Version 4.0.0alpha3, released 1 Dec 2013.
+#
+# See http://backuppc.sourceforge.net.
+#
+#========================================================================
+
+package BackupPC::CGI::JSONStatus;
+
+use strict;
+use BackupPC::CGI::Lib qw(:all);
+use JSON;
+
+sub action
+{
+ GetStatusInfo("info jobs hosts queueLen");
+ my $output;
+ my $host = $1 if ( $In{host} =~ /(.*)/ );
+
+ if(! defined($host) || $host eq "") {
+ $output = encode_json \%Status;
+ } elsif ($host eq "server") {
+ $output = encode_json \%Info;
+ } elsif (! defined($Status{$host})) {
+ $output = encode_json {
+ 'status' => JSON::false,
+ 'message' => 'Host does not exist',
+ 'error' => JSON::true
+ };
+ } else {
+ $bpc->ConfigRead($host);
+ %Conf = $bpc->Conf();
+ $Status{$host}{config} = {
+ 'ip' => $Conf{ClientNameAlias},
+ 'disabled' => $Conf{BackupsDisable}
+ };
+ $output = encode_json $Status{$host};
+ }
+
+ print 'Content-type: application/json', "\r\n\r\n",
+ $output;
+}
+
+1;
diff --git a/lib/BackupPC/CGI/ModifyHost.pm b/lib/BackupPC/CGI/ModifyHost.pm
new file mode 100644
index 0000000..80146da
--- /dev/null
+++ b/lib/BackupPC/CGI/ModifyHost.pm
@@ -0,0 +1,89 @@
+#============================================================= -*-perl-*-
+#
+# BackupPC::CGI::ModifyHost package
+#
+# DESCRIPTION
+#
+# This module implements an interface to modify hosts via external call.
+#
+# AUTHOR
+# Nicholas Hall <[email protected]>
+#
+# COPYRIGHT
+# Copyright (C) 2003-2013 Craig Barratt
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#========================================================================
+#
+# Version 4.0.0alpha3, released 1 Dec 2013.
+#
+# See http://backuppc.sourceforge.net.
+#
+#========================================================================
+
+package BackupPC::CGI::ModifyHost;
+
+use strict;
+use BackupPC::CGI::Lib qw(:all);
+use JSON;
+
+sub action {
+ my %output;
+ my $host = $1 if ( $In{host} =~ /(.*)/ );
+ my $disable = $1 if ( $In{disable} =~ /(0|1)/ );
+ my $ip = $1 if ( $In{ip} =~ /(10\.100\.[\.\d]+)/ );
+
+ print 'Content-type: application/json', "\r\n\r\n";
+
+ if ($host eq "") {
+ $output{'status'} = JSON::false;
+ $output{'message'} = "Missing paramenter 'host' in request";
+
+ print encode_json \%output;
+ return 0;
+ }
+
+ if ($disable eq "" && $ip eq "") {
+ $output{'status'} = JSON::false;
+ $output{'message'} = "Missing parameter 'disable' and/or 'ip' in request";
+
+ print encode_json \%output;
+ return 0;
+ }
+
+ # Update host config
+ my $file = $bpc->ConfDir() . "/pc/${host}.pl";
+ local $^I = '.old';
+ local @ARGV = ( $file );
+ while(<>) {
+ if ($disable ne "") {
+ s/{BackupsDisable} = [01];/{BackupsDisable} = ${disable};/;
+ $bpc->ServerMesg("log JSON updated ${host} BackupsDisable to ${disable}");
+ }
+
+ if ($ip ne "") {
+ s/{ClientNameAlias} = .*/{ClientNameAlias} = '${ip}';/;
+ $bpc->ServerMesg("log JSON updated ${host} ClientNameAlias to ${ip}");
+ }
+
+ print;
+ }
+
+ $output{'status'} = JSON::true;
+ $output{'message'} = "Host was modified successfully";
+ print encode_json \%output;
+}
+
+1;
--
2.1.4
------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
BackupPC-users mailing list
[email protected]
List: https://lists.sourceforge.net/lists/listinfo/backuppc-users
Wiki: http://backuppc.wiki.sourceforge.net
Project: http://backuppc.sourceforge.net/