Skip to content

Instantly share code, notes, and snippets.

@cweiland
Created June 23, 2021 11:06
Show Gist options
  • Save cweiland/5b3d91c5ac8bdaebe8f32831202cded6 to your computer and use it in GitHub Desktop.
Save cweiland/5b3d91c5ac8bdaebe8f32831202cded6 to your computer and use it in GitHub Desktop.
Oneprovider-pfsense-opnsense-ipv4-vip-failover.php
<?php
#!/usr/local/bin/php
require_once("config.inc");
require_once("interfaces.inc");
require_once("util.inc");
$api_key = "API_KEY";
$client_key = "CLIENT_KEY";
$IPFO = "IP_VIP_FO";
$hostname = gethostname();
switch ($hostname) {
case "SERVERNAME1":
$WAN_IP="WAN_IP";
$SERVER_ID="SERVER_ID";
break;
default :
log_error("Strange error");
}
$subsystem = !empty($argv[1]) ? $argv[1] : '';
$type = !empty($argv[2]) ? $argv[2] : '';
if ($type != 'MASTER' && $type != 'BACKUP') {
log_error("Carp '$type' event unknown from source '{$subsystem}'");
exit(1);
}
if (!strstr($subsystem, '@')) {
log_error("Carp '$type' event triggered from wrong source '{$subsystem}'");
exit(1);
}
list ($vhid, $iface) = explode('@', $subsystem);
$friendly = convert_real_interface_to_friendly_interface_name($iface);
$carp_iface = "{$friendly}_vip{$vhid}";
$descr = convert_friendly_interface_to_friendly_descr($carp_iface);
log_error(sprintf('Carp cluster member "%s (%s)" has resumed the state "%s" for vhid %s', $descr, $subsystem, $type, $vhid));
if ($carp_iface != 'opt3_vip3' || $type != 'MASTER') {
log_error("Nothing to do");
exit(0);
}
function tolog($data, $console=true){
log_error("$data");
if($console){
log_error("\n");
}
}
function call_api($http_method, $endpoint, $get = array() , $post = array())
{
$api_key = "API_d0VBQ33ToALutGM89pjOEwBjU29euMJl";
$client_key = "CK_50ILDNZZoZJrAIHzui7dT6UVMs5baoCP";
if (!empty($get))
{
$endpoint .= '?' . http_build_query($get);
}
$call = curl_init();
curl_setopt($call, CURLOPT_URL, 'https://api.oneprovider.com' . $endpoint);
curl_setopt($call, CURLOPT_HTTPHEADER, array(
'Api-Key: ' . $api_key,
'Client-Key: ' . $client_key,
'X-Pretty-JSON: 1'
));
curl_setopt($call, CURLOPT_RETURNTRANSFER, true);
if ($http_method == 'POST')
{
curl_setopt($call, CURLOPT_POST, true);
curl_setopt($call, CURLOPT_POSTFIELDS, http_build_query($post));
}
elseif ($http_method == 'DELETE')
{
curl_setopt($call, CURLOPT_CUSTOMREQUEST, $http_method);
}
$result = curl_exec($call);
return $result;
}
function errorcorr($jsonobj){
if(isset($jsonobj->result) && $jsonobj->result == "success"){
tolog("Done");
return 0;
} else {
if(isset($jsonobj->error) && isset($jsonobj->error->message)){
tolog("Error :");
tolog($jsonobj->error->message);
return -1;
} else {
tolog("Error : Unknown");
tolog(var_dump($jsonobj));
exit(-2);
}
}
}
function delete_mac($server_id, $ip){
$ipfoaction = array(
'server_id' => $server_id,
'address' => $ip,
'action' => 'delete_mac'
);
$result = call_api('POST', '/server/action/', array() , $ipfoaction);
return json_decode($result);
}
function moveip($server_id, $ip){
$ipfoaction = array(
'server_id' => $server_id,
'source' => $ip,
'action' => 'toggle_failover'
);
$result = call_api('POST', '/server/action/', array() , $ipfoaction);
return json_decode($result);
}
function clonemac($server_id, $ipsrc, $iptrg){
$ipfoaction = array(
'server_id' => $server_id,
'action' => 'duplicate_mac',
'address' => $ipsrc,
'target' => $iptrg
);
$result = call_api('POST', '/server/action/', array() , $ipfoaction);
return json_decode($result);
}
tolog("Delete $IPFO MAC Address");
$result=delete_mac($SERVER_ID, $IPFO);
errorcorr($result);
do {
sleep(10);
tolog("Move $IPFO to $SERVER_ID");
$result=moveip($SERVER_ID, $IPFO);
} while(errorcorr($result) == -1 && $result->error->message != "You already have assigned all the IPs allowed for this server.");
do{
sleep(10);
tolog("Clone $WAN_IP to $IPFO");
$result=clonemac($SERVER_ID, $WAN_IP, $IPFO);
} while(errorcorr($result) == -1);
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment