系统更改导致自定义咆哮发送脚本发送空白消息正文
我使用 class.growl.php 在我的 debian 机器上向自己发送服务器更新,它发送正文内容很好,但在 centos 5.6 上,当从 cron 运行时,它发送空正文,但手动运行时很好 class.growl.php:
<?PHP
class Growl
{
const GROWL_PRIORITY_LOW = -2;
const GROWL_PRIORITY_MODERATE = -1;
const GROWL_PRIORITY_NORMAL = 0;
const GROWL_PRIORITY_HIGH = 1;
const GROWL_PRIORITY_EMERGENCY = 2;
private $appName;
private $address;
private $notifications;
private $password;
private $port;
public function __construct($address, $password = '', $app_name = 'PHP Growl')
{
$this->appName = utf8_encode($app_name);
$this->address = $address;
$this->notifications = array();
$this->password = $password;
$this->port = 9887;
}
public function addNotification($name, $enabled = true)
{
$this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
}
public function register()
{
$data = '';
$defaults = '';
$num_defaults = 0;
for($i = 0; $i < count($this->notifications); $i++)
{
$data .= pack('n', strlen($this->notifications[$i]['name'])) . $this->notifications[$i]['name'];
if($this->notifications[$i]['enabled'])
{
$defaults .= pack('c', $i);
$num_defaults++;
}
}
// pack(Protocol version, type, app name, number of notifications to register)
$data = pack('c2nc2', 1, 0, strlen($this->appName), count($this->notifications), $num_defaults) . $this->appName . $data . $defaults;
$data .= pack('H32', md5($data . $this->password));
return $this->send($data);
}
public function notify($name, $title, $message, $priority = 0, $sticky = false)
{
$name = utf8_encode($name);
$title = utf8_encode($title);
$message = utf8_encode($message);
$priority = intval($priority);
$flags = ($priority & 7) * 2;
if($priority < 0) $flags |= 8;
if($sticky) $flags |= 256;
// pack(protocol version, type, priority/sticky flags, notification name length, title length, message length. app name length)
$data = pack('c2n5', 1, 1, $flags, strlen($name), strlen($title), strlen($message), strlen($this->appName));
$data .= $name . $title . $message . $this->appName;
$data .= pack('H32', md5($data . $this->password));
return $this->send($data);
}
private function send($data)
{
if(function_exists('socket_create') && function_exists('socket_sendto'))
{
$sck = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);
return true;
}
elseif(function_exists('fsockopen'))
{
$fp = fsockopen('udp://' . $this->address, $this->port);
fwrite($fp, $data);
fclose($fp);
return true;
}
return false;
}
}
check.sh:
#!/bin/sh
SERVICE='httpd'
//Debian SERVICE='apache2'
echo $(service httpd status) > stat.txt
//Debian echo $(/etc/init.d/apache2 status) > stat.txt
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
php -q ~/growl/send.php Run 'Notification' 'httpd is Running fine' // Apache2
else
php -q ~/growl/send.php Error 'Error' 'httpd is Down' // Apache2
fi
#rm stat.txt
SERVICE='mysqld'
//Debian SERVICE='mysql'
echo $(service mysqld status) > stat.txt //Debian mysql
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
php -q ~/growl/send.php Run 'Notification' 'Mysqld is Running fine' //mysql
else
php -q ~/growl/send.php Error 'Error' 'Mysqld is Down' //mysql
fi
#rm stat.txt
Send.php:
<?php
include_once dirname(__FILE__).'/class.growl.php';
// check for all required arguments
// first argument is always name of script!
if ($argv[1] == 'ls'){
echo "Test\n";
echo "Error\n";
echo "Run\n";
exit (0);
}
if ($argc != 4) {
die("Usage: send.php <Notification To Send 'ls to list'> <Title> <Message>\n");
}
// remove first argument
array_shift($argv);
$ApacheFile = 'stat.txt';
$Notification = $argv[0];
$Title = $argv[1];
$Message = $argv[2];
if (file_exists($ApacheFile)){
$fh = fopen($ApacheFile, 'r');
$theData = fread($fh, filesize($ApacheFile));
fclose($fh);
$Message = $theData;
}
$growl = new Growl('192.168.1.99', 'scorpio', 'Notify');
$growl->addNotification('Test');
$growl->addNotification('Error');
$growl->addNotification('Run');
$growl->register();
if ($Notification == 'Test'){
$growl->notify($Notification, $Title, $Message);
}else if ($Notification == 'Error'){
$growl->notify($Notification, $Title, $Message);
}else if($Notification == 'Run'){
$growl->notify($Notification, $Title, $Message);
}
cron: */5 * * * * /root/growl/check.sh
I am using class.growl.php to send myself server updates on my debian machine it sends the body content fine but on centos 5.6 it sends them with an empty body when run from cron but fine when run manually
class.growl.php:
<?PHP
class Growl
{
const GROWL_PRIORITY_LOW = -2;
const GROWL_PRIORITY_MODERATE = -1;
const GROWL_PRIORITY_NORMAL = 0;
const GROWL_PRIORITY_HIGH = 1;
const GROWL_PRIORITY_EMERGENCY = 2;
private $appName;
private $address;
private $notifications;
private $password;
private $port;
public function __construct($address, $password = '', $app_name = 'PHP Growl')
{
$this->appName = utf8_encode($app_name);
$this->address = $address;
$this->notifications = array();
$this->password = $password;
$this->port = 9887;
}
public function addNotification($name, $enabled = true)
{
$this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
}
public function register()
{
$data = '';
$defaults = '';
$num_defaults = 0;
for($i = 0; $i < count($this->notifications); $i++)
{
$data .= pack('n', strlen($this->notifications[$i]['name'])) . $this->notifications[$i]['name'];
if($this->notifications[$i]['enabled'])
{
$defaults .= pack('c', $i);
$num_defaults++;
}
}
// pack(Protocol version, type, app name, number of notifications to register)
$data = pack('c2nc2', 1, 0, strlen($this->appName), count($this->notifications), $num_defaults) . $this->appName . $data . $defaults;
$data .= pack('H32', md5($data . $this->password));
return $this->send($data);
}
public function notify($name, $title, $message, $priority = 0, $sticky = false)
{
$name = utf8_encode($name);
$title = utf8_encode($title);
$message = utf8_encode($message);
$priority = intval($priority);
$flags = ($priority & 7) * 2;
if($priority < 0) $flags |= 8;
if($sticky) $flags |= 256;
// pack(protocol version, type, priority/sticky flags, notification name length, title length, message length. app name length)
$data = pack('c2n5', 1, 1, $flags, strlen($name), strlen($title), strlen($message), strlen($this->appName));
$data .= $name . $title . $message . $this->appName;
$data .= pack('H32', md5($data . $this->password));
return $this->send($data);
}
private function send($data)
{
if(function_exists('socket_create') && function_exists('socket_sendto'))
{
$sck = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);
return true;
}
elseif(function_exists('fsockopen'))
{
$fp = fsockopen('udp://' . $this->address, $this->port);
fwrite($fp, $data);
fclose($fp);
return true;
}
return false;
}
}
check.sh:
#!/bin/sh
SERVICE='httpd'
//Debian SERVICE='apache2'
echo $(service httpd status) > stat.txt
//Debian echo $(/etc/init.d/apache2 status) > stat.txt
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
php -q ~/growl/send.php Run 'Notification' 'httpd is Running fine' // Apache2
else
php -q ~/growl/send.php Error 'Error' 'httpd is Down' // Apache2
fi
#rm stat.txt
SERVICE='mysqld'
//Debian SERVICE='mysql'
echo $(service mysqld status) > stat.txt //Debian mysql
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
php -q ~/growl/send.php Run 'Notification' 'Mysqld is Running fine' //mysql
else
php -q ~/growl/send.php Error 'Error' 'Mysqld is Down' //mysql
fi
#rm stat.txt
Send.php:
<?php
include_once dirname(__FILE__).'/class.growl.php';
// check for all required arguments
// first argument is always name of script!
if ($argv[1] == 'ls'){
echo "Test\n";
echo "Error\n";
echo "Run\n";
exit (0);
}
if ($argc != 4) {
die("Usage: send.php <Notification To Send 'ls to list'> <Title> <Message>\n");
}
// remove first argument
array_shift($argv);
$ApacheFile = 'stat.txt';
$Notification = $argv[0];
$Title = $argv[1];
$Message = $argv[2];
if (file_exists($ApacheFile)){
$fh = fopen($ApacheFile, 'r');
$theData = fread($fh, filesize($ApacheFile));
fclose($fh);
$Message = $theData;
}
$growl = new Growl('192.168.1.99', 'scorpio', 'Notify');
$growl->addNotification('Test');
$growl->addNotification('Error');
$growl->addNotification('Run');
$growl->register();
if ($Notification == 'Test'){
$growl->notify($Notification, $Title, $Message);
}else if ($Notification == 'Error'){
$growl->notify($Notification, $Title, $Message);
}else if($Notification == 'Run'){
$growl->notify($Notification, $Title, $Message);
}
cron: */5 * * * * /root/growl/check.sh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论