This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
astdb.pl
executable file
·122 lines (100 loc) · 3.16 KB
/
astdb.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
#!/usr/bin/perl
use Asterisk::AMI::Common;
use Data::Dumper;
use strict;
use warnings;
#Database to replicate
my %database = ( TestFamily => { TestKey1 => 1,
TestKey2 => 2,
TestKey3 => 3,
TestKey4 => 4,
TestKey5 => 5 });
my $astman = Asterisk::AMI::Common->new( PeerAddr => '192.168.56.3',
username => 'test',
secret => 'work');
die "Unable to connect to asterisk" unless ($astman);
#Remove Potential conflicting databases
foreach (keys %database) {
die "Failed to remove database $_" unless ($astman->db_deltree($_));
}
#Populate database
foreach my $family (keys %database) {
while (my ($key, $val) = each %{$database{$family}}) {
die "Unable to insert $family/$key with value $val" unless ($astman->db_put($family, $key, $val));
print "Inserted $family/$key with value $val\n";
}
}
#Check values
foreach my $family (keys %database) {
while (my ($key, $val) = each %{$database{$family}}) {
my $retval = $astman->db_get($family, $key, $val);
if (!defined $retval) {
die "Unable to retrive $family/$key";
}
if ($retval != $val) {
print "db_get value for $family/$key does not match (should be $val was $retval)\n";
} else {
print "db_get value for $family/$key matches\n";
}
}
}
#Get database via db_show
my $db = $astman->db_show();
die "Unable to retrieve database" unless ($db);
#Compare it to what we think it should be
foreach my $family (keys %database) {
my %dbfam = %{$db->{$family}};
while (my ($key, $val) = each %{$database{$family}}) {
if ($dbfam{$key} != $val) {
print "db_show value for $family/$key does not match (should be $val was $dbfam{$key})\n";
} else {
print "db_show value for $family/$key matches\n";
}
}
}
#Delete entries with db_del
foreach my $family (keys %database) {
foreach my $key (keys %{$database{$family}}) {
die "Unable to delete $family/$key" unless ($astman->db_del($family, $key));
}
}
#Check values after delete
foreach my $family (keys %database) {
while (my ($key, $val) = each %{$database{$family}}) {
my $retval = $astman->db_get($family, $key, $val);
if (!defined $retval) {
print "db_del Succesfully deleted $family/$key\n";
next;
}
if ($retval != $val) {
print "db_del failed and value for $family/$key does not match (should be $val was $retval)\n";
} else {
print "db_del failed and value for $family/$key matches\n";
}
}
}
#Re-Populate database for deltree
foreach my $family (keys %database) {
while (my ($key, $val) = each %{$database{$family}}) {
die "Unable to insert $family/$key with value $val" unless ($astman->db_put($family, $key, $val));
}
}
#Final Cleanup
foreach (keys %database) {
die "Failed to remove database $_" unless ($astman->db_deltree($_));
}
#Check values after delete
foreach my $family (keys %database) {
while (my ($key, $val) = each %{$database{$family}}) {
my $retval = $astman->db_get($family, $key, $val);
if (!defined $retval) {
print "db_deltree succesfully deleted $family/$key\n";
next;
}
if ($retval != $val) {
print "db_deltree failed and value for $family/$key does not match (should be $val was $retval)\n";
} else {
print "db_deltree failed and value for $family/$key matches\n";
}
}
}