Created
November 12, 2012 17:33
-
-
Save symm/4060715 to your computer and use it in GitHub Desktop.
Linode Kernel version alerting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Linode Kernel Version alerter. | |
* | |
* Fetches a list of the available Linode kernels and alerts you if | |
* your currently running version is out of date. | |
* | |
* Crontab: * 23 * * php /path/to/this/script | |
* @author Gareth Jones | |
*/ | |
$kernels = new LinodeKernelList(); | |
$thisKernel = php_uname('r'); | |
$thisArch = php_uname('m'); | |
if ("x86_64" == $thisArch) { | |
$latestKernel = $kernels->getLatest64Bit(); | |
} else { | |
$latestKernel = $kernels->getLatest32Bit(); | |
} | |
if (!strstr($thisKernel, 'linode')) { | |
echo "You don't appear to be running a Linode Kernel: " . $thisKernel . PHP_EOL; | |
exit(1); | |
} | |
if (!strstr($thisKernel, $latestKernel)) { | |
echo 'Your Kernel appears to be out of date' . PHP_EOL; | |
echo 'Current: ' . $thisKernel . PHP_EOL; | |
echo 'Latest: ' . $latestKernel . PHP_EOL; | |
} | |
class LinodeKernelList | |
{ | |
private $kernels; | |
private $latest32bit; | |
private $latest64bit; | |
private $rawFeed; | |
public function __construct($feedUrl = "http://www.linode.com/kernels/rss.xml") | |
{ | |
$this->kernels = array(); | |
$this->rawFeed = @file_get_contents($feedUrl); | |
if ($this->rawFeed == false) { | |
throw new \Exception('Unable to get the Linode Kernels RSS feed.'); | |
} | |
$this->parseFeed(); | |
} | |
public function getLatest32Bit() | |
{ | |
return $this->latest32bit; | |
} | |
public function getLatest64Bit() | |
{ | |
return $this->latest64bit; | |
} | |
public function getAll() | |
{ | |
return $this->kernels; | |
} | |
private function parseFeed() | |
{ | |
$xml = simplexml_load_string($this->rawFeed); | |
foreach ($xml->channel->item as $item) { | |
$title = (string) $item->title; | |
if (strstr($title, 'Latest 32')) { | |
$title = $this->parseLatest($title); | |
$this->latest32bit = $title; | |
} elseif (strstr($title, 'Latest 64') ) { | |
$title = $this->parseLatest($title); | |
$this->latest64bit = $title; | |
} | |
if (strstr($title,'x86_64')) { | |
$this->kernels['64 bit'][] = $title; | |
} else { | |
$this->kernels['32 bit'][] = $title; | |
} | |
} | |
} | |
// Latest kernels are listed as: | |
// "Latest 32 bit (3.6.5-linode47)" | |
// "Latest 64 bit (3.6.5-x86_64-linode28)" | |
private function parseLatest($title) | |
{ | |
$res = preg_match('/(?<=\()(.+)(?=\))/is', $title, $matches); | |
return $matches[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment