-
Notifications
You must be signed in to change notification settings - Fork 0
/
sesam2idb.pl
executable file
·165 lines (143 loc) · 5.28 KB
/
sesam2idb.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/perl
################################################################################
#
# Name: sesam2idb.pl
# Lang: perl
# Date: 2016-03-29
# Author: [email protected]
#
# DESCRIPTION:
# This script queries the SEP database for backup data and writes the
# results into IDB.
#
# REQUIREMENTS:
# - JSON (libjson-perl)
# - curl (curl)
#
# This script must be run on the sesam server!
#
#
# last edited 2017-03-27 [email protected]
#
################################################################################
use warnings;
use strict;
use JSON;
use Data::Dumper;
use Getopt::Std;
use vars qw(%opt);
###################
## Config start
# IDB URL
my $idb_url = 'https://idb.domain.tld/api/v2/machines';
# IDB API Token
my $idb_api_token = '';
# Sesam sm_db binary (full path)
my $sm_db = '/opt/sesam/bin/sesam/sm_db';
# Sesam profile (full path)
my $sm_profile = '/var/opt/sesam/var/ini/sesam2000.profile';
# curl binary (full path)
my $curl = '/usr/bin/curl';
## Config end
###################
###################
## Main start
my $DEBUG = 0;
getopts('dh', \%opt) or &help();
$opt{'h'} and &help();
$opt{'d'} and $DEBUG = 1;
my $now = qx{date}; chomp($now);
my $cmd = 'curl -s -k -X GET '.$idb_url.'?idb_api_token='.$idb_api_token;
$DEBUG and print "DEBUG: cmd = $cmd\n";
my $res_json = qx{$cmd};
my $res = from_json($res_json);
foreach my $server (@{$res}) {
print "Server: ".$server->{fqdn}."\n";
my $skip = 0;
# prepare sesam database queries
my $query_f = "select sesam_date,stop_time,data_size,task FROM results WHERE client like '".$server->{fqdn}."%' AND copy_from IS NULL AND fdi_type='F' ORDER BY sesam_date DESC limit 1";
my $query_d = "select sesam_date,stop_time,data_size,task FROM results WHERE client like '".$server->{fqdn}."%' AND copy_from IS NULL AND fdi_type='D' ORDER BY sesam_date DESC limit 1";
my $query_i = "select sesam_date,stop_time,data_size,task FROM results WHERE client like '".$server->{fqdn}."%' AND copy_from IS NULL AND fdi_type='I' ORDER BY sesam_date DESC limit 1";
my $cmd_f = '. '.$sm_profile.' && '.$sm_db.' "'.$query_f.'"';
my $cmd_d = '. '.$sm_profile.' && '.$sm_db.' "'.$query_d.'"';
my $cmd_i = '. '.$sm_profile.' && '.$sm_db.' "'.$query_i.'"';
# check for full backups
$DEBUG and print "DEBUG: cmd = $cmd_f\n";
my $res_f = qx{$cmd_f};
my ($tmp_date, $tmp_size, $date_d, $size_d, $date_f, $size_f, $date_i, $size_i);
if ($res_f =~ /SUCCESS/) {
$DEBUG and print "DEBUG: Type: Full\n";
if ($res_f =~ /MSG=1/) {
my @fields = split('\|', $res_f);
($tmp_date, $date_f) = split('=', $fields[2]);
$DEBUG and print "DEBUG: Date: $date_f\n";
($tmp_size, $size_f) = split('=', $fields[3]);
$DEBUG and print "DEBUG: Size: $size_f\n";
print " Type: FULL - Date: $date_f - Size: $size_f\n";
my $upd_json = '{"fqdn": "'.$server->{fqdn}.'", "backup_last_full_run": "'.$date_f.'", "backup_last_full_size": "'.$size_f.'"}';
$DEBUG and print "DEBUG: JSON = $upd_json\n";
my $upd_cmd = $curl.' -s -k -g -X PUT -H "Content-Type: application/json" -d \''.$upd_json.'\' '.$idb_url.'?idb_api_token='.$idb_api_token;
$DEBUG and print "DEBUG: cmd = $upd_cmd\n";
qx{$upd_cmd};
} else {
print " Type: FULL - No Backups found!\n";
}
}
# check for differential backups
$DEBUG and print "DEBUG: cmd = $cmd_d\n";
my $res_d = qx{$cmd_d};
if ($res_d =~ /SUCCESS/) {
$DEBUG and print "DEBUG: Type: Diff\n";
if ($res_d =~ /MSG=1/) {
my @fields = split('\|', $res_d);
($tmp_date, $date_d) = split('=', $fields[2]);
$DEBUG and print "DEBUG: Date: $date_d\n";
($tmp_size, $size_d) = split('=', $fields[3]);
$DEBUG and print "DEBUG: Size: $size_d\n";
print " Type: DIFF - Date: $date_d - Size: $size_d\n";
my $upd_json = '{"fqdn": "'.$server->{fqdn}.'", "backup_last_diff_run": "'.$date_d.'", "backup_last_diff_size": "'.$size_d.'"}';
$DEBUG and print "DEBUG: JSON = $upd_json\n";
my $upd_cmd = $curl.' -s -k -g -X PUT -H "Content-Type: application/json" -d \''.$upd_json.'\' '.$idb_url.'?idb_api_token='.$idb_api_token;
$DEBUG and print "DEBUG: cmd = $upd_cmd\n";
qx{$upd_cmd};
} else {
print " Type: DIFF - No Backups found!\n";
}
}
# check for incremental backups
$DEBUG and print "DEBUG: cmd = $cmd_i\n";
my $res_i = qx{$cmd_i};
if ($res_i =~ /SUCCESS/) {
$DEBUG and print "DEBUG: Type: Inc\n";
if ($res_i =~ /MSG=1/) {
my @fields = split('\|', $res_i);
($tmp_date, $date_i) = split('=', $fields[2]);
$DEBUG and print "DEBUG: Date: $date_i\n";
($tmp_size, $size_i) = split('=', $fields[3]);
$DEBUG and print "DEBUG: Size: $size_i\n";
print " Type: INC - Date: $date_i - Size: $size_i\n";
my $upd_json = '{"fqdn": "'.$server->{fqdn}.'", "backup_last_inc_run": "'.$date_i.'", "backup_last_inc_size": "'.$size_i.'"}';
$DEBUG and print "DEBUG: JSON = $upd_json\n";
my $upd_cmd = $curl.' -s -k -g -X PUT -H "Content-Type: application/json" -d \''.$upd_json.'\' '.$idb_url.'?idb_api_token='.$idb_api_token;
$DEBUG and print "DEBUG: cmd = $upd_cmd\n";
qx{$upd_cmd};
} else {
print " Type: INC - No Backups found!\n";
}
}
print "\n";
}
print "Generated $now\n";
## Main end
###################
###################
## Subs start
# print help
sub help {
print " usage: $0 [-d]\n\n";
print " Options:\n";
print "\t-d\t\tenable debug output\n\n";
exit 0;
}
## Subs end
###################