Created
January 1, 2015 21:35
-
-
Save bacinsky/f898b0c450920a7b50b4 to your computer and use it in GitHub Desktop.
Checks for a domain expiration 4 Zabbix
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
#!/usr/bin/env php | |
<?php | |
ini_set('error_log', '/tmp/domain-check.log'); | |
if (empty($argv[1])) { | |
$msg = 'Expected domain name argument'; | |
trigger_error($msg); | |
die($msg . PHP_EOL); | |
} | |
// Arguments | |
$domain = preg_replace('~^www\.~', '', $argv[1]); | |
// Available TLDs | |
$availTld = ['com', 'org', 'sk', 'cz']; | |
// Resolve domain TLD | |
foreach ($availTld as $tld) { | |
if (preg_match('~\.' . preg_quote($tld) . '$~', $domain)) { | |
break; | |
} | |
$tld = null; | |
} | |
// Functions | |
class Whois | |
{ | |
protected $whois; | |
public function __construct($whois) | |
{ | |
$this->whois = $whois; | |
} | |
public function parseDate($label, $pattern = ' (.*)') | |
{ | |
$validDate = []; | |
preg_match('~' . preg_quote($label) . $pattern . '~', $this->whois, $validDate); | |
return $validDate[1]; | |
} | |
} | |
function remaining_days($date) | |
{ | |
return (new \DateTime($date))->diff(new \DateTime)->days; | |
} | |
// Get whois info | |
$whois = new \Whois(shell_exec('whois ' . escapeshellarg($domain))); | |
// Parse whois | |
switch ($tld) { | |
case 'com': | |
$days = remaining_days($whois->parseDate('Expiration Date:')); | |
break; | |
case 'org': | |
$days = remaining_days($whois->parseDate('Registry Expiry Date:')); | |
break; | |
case 'sk': | |
$days = remaining_days($whois->parseDate('Valid-date')); | |
break; | |
case 'cz': | |
$days = remaining_days($whois->parseDate('expire:')); | |
break; | |
} | |
if (!empty($days)) { | |
error_log($domain . ' : ' . $days); | |
die($days . PHP_EOL); | |
} | |
die('Not implemented' . PHP_EOL); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment