1
|
|
|
#!/usr/bin/env php |
2
|
|
|
<?php |
3
|
|
|
|
4
|
|
|
$init_modules = []; |
5
|
|
|
require realpath(__DIR__ . '/..') . '/includes/init.php'; |
6
|
|
|
|
7
|
|
|
/** Bill management tool |
8
|
|
|
* Todo: |
9
|
|
|
* - Actually create a bill |
10
|
|
|
* - Option to empty a bill |
11
|
|
|
* - Probably tons of bug fixes and safety checks. |
12
|
|
|
* Note: |
13
|
|
|
* - Current, this cannot create a new bill. To do this, you need to use the GUI. |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
// Find the correct bill, exit if we get anything other than 1 result. |
17
|
|
|
function list_bills($bill_name) |
18
|
|
|
{ |
19
|
|
|
$bill = dbFetchRows('SELECT `bill_id`,`bill_name` FROM `bills` WHERE `bill_name` LIKE ?', ["$bill_name"]); |
20
|
|
|
if (count($bill) != 1) { |
21
|
|
|
echo "Did not find exactly 1 bill, exiting\n"; |
22
|
|
|
echo 'Query:' . $bill . "\n"; |
|
|
|
|
23
|
|
|
exit(1); |
|
|
|
|
24
|
|
|
} else { |
25
|
|
|
echo "Found bill {$bill[0]['bill_name']} ({$bill[0]['bill_id']})\n"; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $bill[0]['bill_id']; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Create a new bill. |
32
|
|
|
function create_bill($bill_name, $bill_type, $bill_cdr, $bill_day) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
/** create_bill |
35
|
|
|
* Note: |
36
|
|
|
* - bill_name: can be a duplicate since it's unique is is bill_id. We are going to be cowards and refuse to create duplicate bill_name |
37
|
|
|
* - bill_type: can be cdr (Committed data rate, 95th) or quota (total bytes moved) |
38
|
|
|
* - bill_cdr: if bill_type is cdr, then this is in bits, if bill_type is quota, then it's in bytes (!!) |
39
|
|
|
* - bill_day: day of month billing starts. |
40
|
|
|
**/ |
41
|
|
|
echo 'Creating bill with name : ' . $bill_name . ' (Type: ' . $bill_type . ', Quota: ' . $bill_cdr . ")\n"; |
42
|
|
|
$insert = [ |
43
|
|
|
'bill_name' => $bill_name, |
44
|
|
|
'bill_type' => $bill_type, |
45
|
|
|
'bill_cdr' => $bill_cdr, |
46
|
|
|
'bill_day' => '1', |
47
|
|
|
]; |
48
|
|
|
$create_bill = dbInsert($insert, 'bills'); |
49
|
|
|
echo 'Created bill ID ' . $create_bill . "\n"; |
50
|
|
|
|
51
|
|
|
return $create_bill; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// This will get an array of devices we are interested in from the CLI glob |
55
|
|
|
function get_devices($host_glob, $nameType) |
56
|
|
|
{ |
57
|
|
|
return dbFetchRows('SELECT `device_id`,`hostname`,`sysName` FROM `devices` WHERE `' . $nameType . '` LIKE ?', ["%$host_glob%"]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// This will flush bill ports if -r is set on cli |
61
|
|
|
function flush_bill($id) |
62
|
|
|
{ |
63
|
|
|
echo "Removing ports from bill ID $id\n"; |
64
|
|
|
|
65
|
|
|
return dbDelete('bill_ports', '`bill_id` = ?', [$id]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function add_ports_to_bill($devs, $intf_glob, $id) |
69
|
|
|
{ |
70
|
|
|
// Abort mission if no bill id is passed. |
71
|
|
|
if (empty($id)) { |
72
|
|
|
echo "No bill ID passed, exiting...\n"; |
73
|
|
|
exit(1); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Expected interface glob: |
77
|
|
|
echo "Interface glob: $intf_glob\n"; |
78
|
|
|
$device_ids = array_column($devs, 'device_id'); |
79
|
|
|
$ids = implode(',', $device_ids); |
80
|
|
|
|
81
|
|
|
// Find the devices which match the list of IDS and also the interface glob |
82
|
|
|
$query = "SELECT ports.port_id,ports.ifName,ports.ifAlias FROM ports INNER JOIN devices ON ports.device_id = devices.device_id WHERE ifType = 'ethernetCsmacd' AND ports.ifAlias LIKE '%$intf_glob%' AND ports.device_id in ($ids)"; |
83
|
|
|
echo "Query: $query\n"; |
84
|
|
|
foreach (dbFetchRows($query) as $ports) { |
85
|
|
|
echo "Inserting {$ports['ifName']} ({$ports['ifAlias']}) into bill $id\n"; |
86
|
|
|
$insert = [ |
87
|
|
|
'bill_id' => $id, |
88
|
|
|
'port_id' => $ports['port_id'], |
89
|
|
|
'bill_port_autoadded' => '1', |
90
|
|
|
]; |
91
|
|
|
dbInsert($insert, 'bill_ports'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
function print_help() |
98
|
|
|
{ |
99
|
|
|
echo "Usage:\n"; |
100
|
|
|
echo "Updating bills\n"; |
101
|
|
|
echo "-b <bill name glob> Bill name to match\n"; |
102
|
|
|
echo "-s <sysName glob> sysName to match (Cannot be used with -h)\n"; |
103
|
|
|
echo "-h <hostname glob> Hostname to match (Cannot be used with -s)\n"; |
104
|
|
|
echo "-i <Interface description glob> Interface description to match\n"; |
105
|
|
|
echo "-f Flush all ports from a bill before adding adding ports\n"; |
106
|
|
|
echo "Creating bills\n"; |
107
|
|
|
echo "-n Create new bill\n"; |
108
|
|
|
echo "-t bill type (cdr or quota)\n"; |
109
|
|
|
echo "-q Quota (In bits for cdr, bytes for quota)\n\n"; |
110
|
|
|
echo "Update an existing bill called 'Telia - Transit', add interfaces matching \"Telia\" from all devices\n"; |
111
|
|
|
echo "php manage_bills.php -b 'Telia - Transit' -s all -i Telia\n\n"; |
112
|
|
|
echo "Create a new bill called 'Transit' with a CDR of 1Gbit\n"; |
113
|
|
|
echo "php manage_bills.php -n -b 'Transit' -t cdr -q 1000000000"; |
114
|
|
|
echo "\n"; |
115
|
|
|
exit; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** Setup options: |
119
|
|
|
* b - bill_name - bill glob |
120
|
|
|
* i - circuit_id - interface glob |
121
|
|
|
* s - sysName - device glob |
122
|
|
|
* h - hostname - device glob |
123
|
|
|
* f - flush - boolean |
124
|
|
|
* n - new - create new bill |
125
|
|
|
* t - type - bill type |
126
|
|
|
* q - quota - bill quota |
127
|
|
|
**/ |
128
|
|
|
$options = getopt('b:s:h:i:f:np:t:q:'); |
129
|
|
|
|
130
|
|
|
if (! empty($options['s'])) { |
131
|
|
|
$host_glob = str_replace('*', '%', $options['s']); |
132
|
|
|
$nameType = 'sysName'; |
133
|
|
|
} |
134
|
|
|
if (! empty($options['h'])) { |
135
|
|
|
$host_glob = str_replace('*', '%', $options['h']); |
136
|
|
|
$nameType = 'hostname'; |
137
|
|
|
} |
138
|
|
|
if (array_key_exists('n', $options)) { |
139
|
|
|
$create_bill = true; |
140
|
|
|
} |
141
|
|
|
if (! empty($options['t'])) { |
142
|
|
|
$bill_type = $options['t']; |
143
|
|
|
} |
144
|
|
|
if (! empty($options['q'])) { |
145
|
|
|
$bill_cdr = $options['q']; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$bill_name = str_replace('*', '%', $options['b']); |
149
|
|
|
$intf_glob = str_replace('*', '%', $options['i']); |
150
|
|
|
|
151
|
|
|
// Exit if no bill |
152
|
|
|
if (empty($bill_name)) { |
153
|
|
|
echo "Please set -b (bill name)\n"; |
154
|
|
|
print_help(); |
155
|
|
|
} |
156
|
|
|
if ($create_bill) { |
157
|
|
|
create_bill($bill_name, $bill_type, $bill_cdr, '1'); |
158
|
|
|
exit(1); |
159
|
|
|
} |
160
|
|
|
// Exit if missing hostname or sysName (or both set |
161
|
|
|
if (empty($options['s']) && empty($options['h'])) { |
162
|
|
|
echo "Please set -s (sysName) or -h (hosthame)\n"; |
163
|
|
|
print_help(); |
164
|
|
|
} elseif (! empty($options['s']) && ! empty($options['h'])) { |
165
|
|
|
echo "Please set either -s or -h, not both\n"; |
166
|
|
|
print_help(); |
167
|
|
|
} |
168
|
|
|
// Exit if missing hostname or sysName |
169
|
|
|
if (empty($options['i'])) { |
170
|
|
|
echo "Please set -i (interface glob)\n"; |
171
|
|
|
print_help(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($bill_name == 'all') { |
175
|
|
|
$bill_name = '%'; |
176
|
|
|
} |
177
|
|
|
if ($intf_glob == 'all') { |
178
|
|
|
$intf_glob = '%'; |
179
|
|
|
} |
180
|
|
|
if ($host_glob == 'all') { |
181
|
|
|
$host_glob = '%'; |
182
|
|
|
} |
183
|
|
|
if (isset($options['f'])) { |
184
|
|
|
$flush = true; |
185
|
|
|
} else { |
186
|
|
|
$flush = false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$id = list_bills($bill_name); |
190
|
|
|
|
191
|
|
|
$devices = get_devices($host_glob, $nameType); |
192
|
|
|
|
193
|
|
|
if (empty($devices)) { |
194
|
|
|
echo "No devices found\n"; |
195
|
|
|
exit(1); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ($flush) { |
199
|
|
|
$flush_ret = flush_bill($id); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$ret = add_ports_to_bill($devices, $intf_glob, $id); |
203
|
|
|
|