From 731261b509ebbbcf39126f899965f8ea49d92abd Mon Sep 17 00:00:00 2001 From: Ruairi Carroll Date: Sat, 2 Dec 2017 20:26:19 +0000 Subject: [PATCH] feature: Added manage_bills.php script * Adding 1st rev of manage_bills.php * Stlye fixes * Freshinging PR 7633. Changing -r to -f, to be less confusing. Adding code style changes as per @laf's suggesting * Adding in option for sysname or hostname variable. Fixing hostname lookup bug * style fixes * -renaming add_ports to add_ports_to_bill - changing -i to -b for clarity sake - Will print help if -s and -h are not set * Adding check to ensure -i is set via cli * I'm stupid * I'm stupid --- scripts/manage_bills.php | 144 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100755 scripts/manage_bills.php diff --git a/scripts/manage_bills.php b/scripts/manage_bills.php new file mode 100755 index 0000000000..1f8ee6dd22 --- /dev/null +++ b/scripts/manage_bills.php @@ -0,0 +1,144 @@ +#!/usr/bin/env php + $id, + 'port_id' => $ports['port_id'], + 'bill_port_autoadded' => '1' + ); + dbInsert($insert, 'bill_ports'); + } + return true; +} + +/** Setup options: + l - bill_name - bill glob + c - circuit_id - interface glob + s - sysName - device glob + h - hostname - device glob + f - flush - boolean +**/ + +$options = getopt('b:s:h:i:f'); + +if (!empty($options['s'])) { + $host_glob = str_replace('*', '%', mres($options['s'])); + $nameType = "sysName"; +} +if (!empty($options['h'])) { + $host_glob = str_replace('*', '%', mres($options['h'])); + $nameType = "hostname"; +} +if (empty($options['s']) && empty($options['h'])) { + echo "Please set -s or -h\n"; +} else if (!empty($options['s']) && !empty($options['h'])) { + echo "Please set either -s or -h, not both\n"; +} +if (!empty($options['i']) && !empty($options['h'])) { + echo "Please set -i\n"; +} + +$bill_name = str_replace('*', '%', mres($options['b'])); +$intf_glob = str_replace('*', '%', mres($options['i'])); + +if (empty($bill_name) or !(empty($options['h']) and empty($options['s']))) { + echo "Usage:\n"; + echo "-b Bill name to match\n"; + echo "-s sysName to match (Cannot be used with -h)\n"; + echo "-h Hostname to match (Cannot be used with -s)\n"; + echo "-i Interface description to match\n"; + echo "-f Flush all ports from a bill before adding adding ports\n"; + echo "Example:\n"; + echo "If I wanted to add all interfaces containing the description Telia to a bill called 'My Lovely Transit Provider'\n"; + echo "php manage_bills.php -l 'My Lovely Transit Provider' -d all -c Telia"; + echo "\n"; + exit; +} + +if ($bill_name == 'all') { + $bill_name = '%'; +} +if ($intf_glob == 'all') { + $intf_glob = '%'; +} +if ($host_glob == 'all') { + $host_glob = '%'; +} +if (isset($options['f'])) { + $flush = true; +} else { + $flush = false; +} + +$id = list_bills($bill_name); + +$devices = get_devices($host_glob, $nameType); + +if (empty($devices)) { + echo "No devices found\n"; + exit(1); +} + +if ($flush) { + $flush_ret = flush_bill($id); +} + +$ret = add_ports_to_bill($devices, $intf_glob, $id);