-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathngcp-cleanup-sems
executable file
·84 lines (67 loc) · 1.99 KB
/
ngcp-cleanup-sems
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
#!/usr/bin/perl
use strict;
use warnings;
use Redis;
my $max_days = 2;
my $service_restart = 1;
lprint("=====================");
lprint("script start");
my $server_name = 'keydb';
# find bind IP from redis config file
my ($server_ip, $port);
{
my $fd;
open($fd, '<', "/etc/$server_name/$server_name.conf") or die("couldn't open $server_name config: $!");
my @conf = <$fd>;
close($fd);
my @bind = grep {/^bind /} @conf;
@bind or die('no "bind" config directive found');
($server_ip) = $bind[0] =~ /^bind (\S+)/ or die("couldn't parse bind directive: $bind[0]");
my @port = grep {/^port /} @conf;
@port or die('no "port" config directive found');
($port) = $port[0] =~ /^port (\d+)/ or die("couldn't parse port directive: $port[0]");
}
my $r = Redis->new(server => "$server_ip:$port", on_connect => sub {$_[0]->select(0)})
or die("failed to create Redis object");
my $keys = $r->hkeys('sems_globals:cc_sw_prepaid') or die("failed to get keys for sems_globals:cc_sw_prepaid");
my $cleaned = 0;
for my $key (@{$keys}) {
my $line = $r->hget('sems_globals:cc_sw_prepaid', $key) or next;
my $toks = tokenize($line) or next;
my $attrs = tokenize($toks->[3]) or next;
my $xtime = $attrs->[3] or next;
# check timeout
time() - $xtime < ($max_days * 86400) and next;
# check if call info is present
my $info = $toks->[2];
$r->hgetall($toks->[2]) or next;
lprint("$key has a call since " . localtime($xtime) . " with info $info");
my $ret = $r->hdel('sems_globals:cc_sw_prepaid', $key);
lprint("$key has been cleaned, return code $ret");
$cleaned++;
}
if ($cleaned) {
lprint("cleaned $cleaned calls");
if ($service_restart) {
system qw(ngcp-service restart sems);
lprint("sems restarted");
}
}
else {
lprint("nothing to clean");
}
exit;
sub tokenize {
my ($line) = @_;
my @ret;
while ($line =~ s/^(\d+)://s) {
my $num = $1;
my $sub = substr($line, 0, $num, '');
$line =~ s/^,//s or return;
push(@ret, $sub);
}
return \@ret;
}
sub lprint {
print(localtime() . " - " . $_[0] . "\n");
}