-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrappiste.pl
executable file
·71 lines (59 loc) · 1.87 KB
/
trappiste.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
#!/usr/bin/perl -w
# Trappiste : backup & version network devices with git & SNMP
# By Nico <[email protected]>
# Idea by pnl
# BSD Licensed
use strict;
use SNMP::Trapinfo;
use Git::Repository;
use File::Path;
use Net::SNMP;
use File::Copy;
use AppConfig qw(:expand :argcount);
sub git_store_config
{
my $timestamp = time;
my $host=$_[0];
my $community=$_[1];
my $workdir=$_[2]."/".$host;
my $tftpserver=$_[3];
my $filename = $workdir."/".$host.".txt";
my $r;
if (-d $workdir)
{
chdir($workdir);
$r=Git::Repository->new(work_tree => $workdir);
}
else
{
mkpath($workdir);
$r=Git::Repository->create(init => $workdir);
open(F,">",$filename);
print F "\n";
close(F);
chdir($workdir);
$r->run(add => ".");
$r->command(commit => "-m", "empty file automated import");
$r->command(log => '--pretty=oneline', '--all');
}
my ($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => $community,
-nonblocking => 0,
-debug => 1,
-version => "snmpv1"
);
my $result = $session->set_request( -varbindlist => [ ".1.3.6.1.4.1.9.2.1.55.".$tftpserver, OCTET_STRING, $host.".txt" ] );
move("/home/tftpboot/".$host.".txt", $workdir);
$r->command(commit => "-am", "automated commit by ".$0);
}
my $trap = SNMP::Trapinfo->new(*STDIN);
my $config = AppConfig->new();
$config->define('backupdir=s');
$config->define('community=s');
$config->define('tftpserver=s');
$config->file("/opt/scripts/etc/trappiste.conf");
# backup lors d'un write
if (($trap->trapname eq 'CISCO-CONFIG-MAN-MIB::ciscoConfigManEvent') and ($trap->data->{"CISCO-CONFIG-MAN-MIB::ccmHistoryEventConfigDestination"} eq 'startup')) {
git_store_config($trap->hostname,$config->community(),$config->backupdir(),$config->tftpserver());
}