mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
feature: Add a new locking framework that uses flock. (#6858)
Change locks to use flock, as pid checking is not sufficient when PID Namespaces are involved.
This commit is contained in:
committed by
Tony Murray
parent
f02b551145
commit
701fbbc29b
95
LibreNMS/FileLock.php
Normal file
95
LibreNMS/FileLock.php
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LibreNMS
|
||||||
|
*
|
||||||
|
* This file is part of LibreNMS.
|
||||||
|
*
|
||||||
|
* @package LibreNMS
|
||||||
|
* @subpackage FileLock
|
||||||
|
* @copyright (C) 2017
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace LibreNMS;
|
||||||
|
|
||||||
|
class FileLock
|
||||||
|
{
|
||||||
|
private $name = "";
|
||||||
|
private $file = "";
|
||||||
|
/**
|
||||||
|
* @var resource | false
|
||||||
|
*/
|
||||||
|
private $handle = false;
|
||||||
|
private $acquired = false;
|
||||||
|
|
||||||
|
private function __construct($lock_name)
|
||||||
|
{
|
||||||
|
global $config;
|
||||||
|
|
||||||
|
$this->name = $lock_name;
|
||||||
|
$this->file = "$config[install_dir]/.$lock_name.lock";
|
||||||
|
$this->handle = fopen($this->file, "w+");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
$this->release();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Release the lock.
|
||||||
|
*/
|
||||||
|
public function release()
|
||||||
|
{
|
||||||
|
if (!$this->acquired) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->handle !== false) {
|
||||||
|
flock($this->handle, LOCK_UN);
|
||||||
|
fclose($this->handle);
|
||||||
|
}
|
||||||
|
if (file_exists($this->file)) {
|
||||||
|
unlink($this->file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a lock name, try to acquire the lock.
|
||||||
|
* On success return a FileLock object, or on failure return false.
|
||||||
|
* @param string $lock_name Name of lock
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function lock($lock_name)
|
||||||
|
{
|
||||||
|
$lock = new self($lock_name);
|
||||||
|
if ($lock->handle === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flock($lock->handle, LOCK_EX | LOCK_NB)) {
|
||||||
|
$lock->acquired = true;
|
||||||
|
return $lock;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a lock name, try to acquire the lock, exiting on failure.
|
||||||
|
* On success return a FileLock object.
|
||||||
|
* @param string $lock_name Name of lock
|
||||||
|
* @return FileLock
|
||||||
|
*/
|
||||||
|
public static function lockOrDie($lock_name)
|
||||||
|
{
|
||||||
|
$lock = self::lock($lock_name);
|
||||||
|
|
||||||
|
if ($lock === false) {
|
||||||
|
echo "Failed to acquire lock $lock_name, exiting\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return $lock;
|
||||||
|
}
|
||||||
|
}
|
@@ -30,7 +30,7 @@ require __DIR__ . '/includes/init.php';
|
|||||||
|
|
||||||
$options = getopt('d::');
|
$options = getopt('d::');
|
||||||
|
|
||||||
set_lock('alerts');
|
$alerts_lock = \LibreNMS\FileLock::lockOrDie('alerts');
|
||||||
|
|
||||||
if (isset($options['d'])) {
|
if (isset($options['d'])) {
|
||||||
echo "DEBUG!\n";
|
echo "DEBUG!\n";
|
||||||
@@ -60,4 +60,4 @@ if (!defined('TEST') && $config['alert']['disable'] != 'true') {
|
|||||||
echo 'End : '.date('r')."\r\n";
|
echo 'End : '.date('r')."\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
release_lock('alerts');
|
$alerts_lock->release();
|
||||||
|
@@ -33,7 +33,7 @@ if (isset($options['h'])) {
|
|||||||
$where = ' ';
|
$where = ' ';
|
||||||
$doing = 'all';
|
$doing = 'all';
|
||||||
} elseif ($options['h'] == 'new') {
|
} elseif ($options['h'] == 'new') {
|
||||||
set_lock('new-discovery');
|
$new_discovery_lock = \LibreNMS\FileLock::lockOrDie('new-discovery');
|
||||||
$where = 'AND `last_discovered` IS NULL';
|
$where = 'AND `last_discovered` IS NULL';
|
||||||
$doing = 'new';
|
$doing = 'new';
|
||||||
} elseif ($options['h']) {
|
} elseif ($options['h']) {
|
||||||
@@ -140,10 +140,13 @@ if ($discovered_devices) {
|
|||||||
if ($doing === 'new') {
|
if ($doing === 'new') {
|
||||||
// We have added a new device by this point so we might want to do some other work
|
// We have added a new device by this point so we might want to do some other work
|
||||||
oxidized_reload_nodes();
|
oxidized_reload_nodes();
|
||||||
release_lock('new-discovery');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($doing === 'new') {
|
||||||
|
$new_discovery_lock->release();
|
||||||
|
}
|
||||||
|
|
||||||
$string = $argv[0]." $doing ".date($config['dateformat']['compact'])." - $discovered_devices devices discovered in $proctime secs";
|
$string = $argv[0]." $doing ".date($config['dateformat']['compact'])." - $discovered_devices devices discovered in $proctime secs";
|
||||||
d_echo("$string\n");
|
d_echo("$string\n");
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user