added random string to make log file clear of running instances
This commit is contained in:
@@ -14,13 +14,25 @@ require_once('plugins.inc.d/webgui.inc');
|
||||
# Variables #
|
||||
#############
|
||||
|
||||
$wanInterface = 'wan';
|
||||
$wanInterface = 'lan';
|
||||
$gatewayName = 'WAN_GW';
|
||||
|
||||
###################
|
||||
# Start of Script #
|
||||
###################
|
||||
|
||||
function getRandString($n) {
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$randomString = '';
|
||||
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
$index = rand(0, strlen($characters) - 1);
|
||||
$randomString .= $characters[$index];
|
||||
}
|
||||
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
function write_config_and_restart_services ($interface) {
|
||||
write_config();
|
||||
system_hosts_generate(true);
|
||||
@@ -33,17 +45,23 @@ function write_config_and_restart_services ($interface) {
|
||||
rrd_configure(true);
|
||||
}
|
||||
|
||||
function echo_write_log ($message) {
|
||||
function echo_write_log ($randomString, $message) {
|
||||
$dateTime = date("Y-m-d H:i:s");
|
||||
$folder = '/var/log/opnsense_carp_dhcp';
|
||||
$message = $dateTime . " - " . $message;
|
||||
$message = $dateTime . " - " . $randomString . " - " . $message . "\n";
|
||||
if (!is_dir($folder)) {
|
||||
mkdir($folder);
|
||||
}
|
||||
file_put_contents("$folder/log_".date("Ymd").".log", $message, FILE_APPEND);
|
||||
echo $message . PHP_EOL;
|
||||
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);
|
||||
|
||||
$intf_details = legacy_interfaces_details();
|
||||
$intf = get_real_interface($wanInterface);
|
||||
# loop through carp addresses and update the wan ip address
|
||||
@@ -52,26 +70,28 @@ foreach ($config['virtualip']['vip'] as $i => $vip) {
|
||||
$carpStatus = $intf_details[$intf]['carp'][$vip['vhid']]['status'];
|
||||
if ($carpStatus == "BACKUP") {
|
||||
$description = $config['virtualip']['vip'][$i]['descr'];
|
||||
echo_write_log("Description from VIP WAN: " . $description);
|
||||
echo_write_log($randomString, "Description from VIP WAN: " . $description);
|
||||
# VIP WAN (GW: $gatewayIP)
|
||||
$gatewayIP = trim(substr(explode(":", $description)[1], 0, -1));
|
||||
echo_write_log("CARP Gateway IP from Description: " . $gatewayIP);
|
||||
echo_write_log($randomString, "CARP Gateway IP from Description: " . $gatewayIP);
|
||||
if (filter_var($gatewayIP, FILTER_VALIDATE_IP)) {
|
||||
foreach ($config['gateways']['gateway_item'] as $i => $gateway) {
|
||||
if ($gateway['name'] == $gatewayName) {
|
||||
echo_write_log("Current Gateway IP: " . $gateway['gateway']);
|
||||
echo_write_log($randomString, "Current Gateway IP: " . $gateway['gateway']);
|
||||
if ($gateway['gateway'] != $gatewayIP) {
|
||||
echo_write_log("Updating Gateway IPs");
|
||||
echo_write_log($randomString, "Updating Gateway IPs");
|
||||
$config['gateways']['gateway_item'][$i]['gateway'] = $gatewayIP;
|
||||
write_config_and_restart_services($wanInterface);
|
||||
} else {
|
||||
echo_write_log("Gateway IPs are the same, not updating.");
|
||||
echo_write_log($randomString, "Gateway IPs are the same, not updating.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo_write_log("Router is currenty carp master, not updating");
|
||||
echo_write_log($randomString, "Router is currently carp master, not updating");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo_write_log($randomString, "End of Script " . $argv[0]);
|
||||
Reference in New Issue
Block a user