From befbd0429eca3d10c0e807182d5e500ec8b4cf07 Mon Sep 17 00:00:00 2001 From: John Gaunt Date: Sat, 4 Dec 2021 13:31:40 -0500 Subject: [PATCH] changed the way the random string for the log is used to make the function call easier overall --- opnsense-carp-dhcp-backup-gateway.php | 42 ++++++++++++++------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/opnsense-carp-dhcp-backup-gateway.php b/opnsense-carp-dhcp-backup-gateway.php index 8dad003..0782daa 100644 --- a/opnsense-carp-dhcp-backup-gateway.php +++ b/opnsense-carp-dhcp-backup-gateway.php @@ -21,6 +21,8 @@ $gatewayName = 'WAN_GW'; # Start of Script # ################### +$randomString = getRandString(5); + function getRandString($n) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; @@ -45,10 +47,10 @@ function write_config_and_restart_services ($interface) { rrd_configure(true); } -function echo_write_log ($randomString, $message) { +function echo_write_log ($message) { $dateTime = date("Y-m-d H:i:s"); $folder = '/var/log/opnsense_carp_dhcp'; - $message = $dateTime . " - " . $randomString . " - " . $message . "\n"; + $message = $dateTime . " - " . global $randomString . " - " . $message . "\n"; if (!is_dir($folder)) { mkdir($folder); } @@ -56,50 +58,50 @@ function echo_write_log ($randomString, $message) { echo $message; } -$randomString = getRandString(5); -echo_write_log($randomString, "Start of Script " . $argv[0]); -echo_write_log($randomString, "WAN Interface: " . $wanInterface); -echo_write_log($randomString, "Name of static WAN Gateway: " . $gatewayName); + +echo_write_log("Start of Script " . $argv[0]); +echo_write_log("WAN Interface: " . $wanInterface); +echo_write_log("Name of static WAN Gateway: " . $gatewayName); $intf_details = legacy_interfaces_details(); $intf = get_real_interface($wanInterface); -echo_write_log($randomString, "Real WAN Interface: " . $intf); -echo_write_log($randomString, "Looping through VIP Carp Interfaces..."); +echo_write_log("Real WAN Interface: " . $intf); +echo_write_log("Looping through VIP Carp Interfaces..."); # loop through carp addresses and update the wan ip address foreach ($config['virtualip']['vip'] as $i => $vip) { if ($vip['interface'] == $wanInterface) { - echo_write_log($randomString, "VIP Carp WAN Interface and vhid: " . convert_friendly_interface_to_friendly_descr($vip['interface']) . "@{$vip['vhid']}"); + echo_write_log("VIP Carp WAN Interface and vhid: " . convert_friendly_interface_to_friendly_descr($vip['interface']) . "@{$vip['vhid']}"); $carpStatus = $intf_details[$intf]['carp'][$vip['vhid']]['status']; - echo_write_log($randomString, "VIP Carp WAN Status: " . $carpStatus); + echo_write_log("VIP Carp WAN Status: " . $carpStatus); if ($carpStatus == "BACKUP") { $description = $config['virtualip']['vip'][$i]['descr']; - echo_write_log($randomString, "Description from VIP WAN: " . $description); + echo_write_log("Description from VIP WAN: " . $description); # VIP WAN (GW: $gatewayIP) $gatewayIP = trim(substr(explode(":", $description)[1], 0, -1)); - echo_write_log($randomString, "CARP Gateway IP from Description: " . $gatewayIP); + echo_write_log("CARP Gateway IP from Description: " . $gatewayIP); if (filter_var($gatewayIP, FILTER_VALIDATE_IP)) { - echo_write_log($randomString, "Looping through Gateways..."); + echo_write_log("Looping through Gateways..."); foreach ($config['gateways']['gateway_item'] as $i => $gateway) { if ($gateway['name'] == $gatewayName) { - echo_write_log($randomString, "Gateway Name: " . $gateway['name']); - echo_write_log($randomString, "Current Gateway IP: " . $gateway['gateway']); + echo_write_log("Gateway Name: " . $gateway['name']); + echo_write_log("Current Gateway IP: " . $gateway['gateway']); if ($gateway['gateway'] != $gatewayIP) { - echo_write_log($randomString, "Updating Gateway IPs"); + echo_write_log("Updating Gateway IPs"); $config['gateways']['gateway_item'][$i]['gateway'] = $gatewayIP; write_config_and_restart_services($wanInterface); } else { - echo_write_log($randomString, "Gateway IPs are the same, not updating"); + echo_write_log("Gateway IPs are the same, not updating"); } } } } else { - echo_write_log($randomString, "Gateway IP is not a valid IP address"); + echo_write_log("Gateway IP is not a valid IP address"); } } else { - echo_write_log($randomString, "Router is currently Carp master, not updating"); + echo_write_log("Router is currently Carp master, not updating"); } } } -echo_write_log($randomString, "End of Script " . $argv[0]); \ No newline at end of file +echo_write_log("End of Script " . $argv[0]); \ No newline at end of file