RealNetworks的Helix Server 9.0.3.916 for Linux版。多台服务器同时提供服务,需要做到负载平衡和服务器的实时监控,要做到以上要求,就需要实时的获取服务器的信息。
在Helix的控制台中发现了Server Monitor中的Custom Logging有个HTTPPOST方式,Helix可以通过HTTP的Post方式将服务器的信息发送出去。
系统环境:
流媒体: Helix Server 9.03 (9.0.3.916)
Web:Apache 2.0.50 + PHP 5.0.2 + MySQL 4.0.21
系统:RedHat Enterprise Linux Advanced Server 3 update 3
1.在MySQL数据库中建立一个库serverstat
2.建立表ServerList
create TABLE `ServerList` (
`ServerID` tinyint(2) unsigned NOT NULL default '0',
`ServerName` varchar(32) NOT NULL default '',
`ServerIP` varchar(16) NOT NULL default '',
`CpuUsage` int(8) unsigned NOT NULL default '0',
`ServerUptime` int(11) NOT NULL default '0',
`AdminPort` varchar(5) NOT NULL default '',
`ClientNum` int(4) unsigned NOT NULL default '0',
`BandWidth` varchar(20) NOT NULL default '',
`InfoTime` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`ServerID`),
UNIQUE KEY `ServerIP` (`ServerIP`)
) TYPE=MyISAM;
在表中添加你的服务器信息,IP地址一定要有的,其他的无所谓
3. PHP程序
以下是获取流媒体服务器信息的一个小程序,程序中使用的mysql.inc.php(PHP操作MySQL的类),config.inc.php中除了数据库的IP、用户、密码以外,什么都没有。
mysql.inc.php
<?php
$dbhost="localhost";
$dbname="datebase"; //数据库名称
$dbuser="root"; //数据库用户
$dbpassword=""; //数据库密码
@mysql_connect($dbhost,$dbuser,$dbpassword) or die("不能连接到MySQL,请修改'mysql.inc.php'");
@mysql_select_db($dbname) or die("不能选择MySQL数据库,请修改'mysql.inc.php'");
?>
<?
include_once("../include/config.inc.php");
include_once("../include/mysql.inc.php");
$db = new SQL();
$db->Host = $dbHostName;
$db->Database = $dbDatabase;
$db->User = $dbUser;
$db->Password = $dbPassword;
$db->connect();
$debug = 0; // 调试,将query_string 输出到server.log文件
$datetime=date("Y-m-d H:i:s");
$clientnum=$_POST['client_count'];
$serverip =$_SERVER['REMOTE_ADDR'];
$cpuusage=$_POST['cpu_usage'];
$bandwidth=$_POST['bandwidth_output'];
$filenum = intval($_POST['filenum']);
$serveruptime = $_POST['server_uptime'];
$query_string = "update ServerList set ServerUptime =".$serveruptime.",ClientNum = ".$clientnum.",InfoTime ='".$datetime."',BandWidth ='".$bandwidth."' where ServerIP = '".$serverip."';";
if($debug){//调试用
$ferror = fopen("server.log","a+");
if(!fwrite($ferror,$query_string."\n")){
exit;
}
}
$db->query($query_string);
$db->free_result();
exit();
?>
显示服务器状态信息的一个测试程序,程序中判断当前时间与服务器报告时间的差,如果超过60秒,基本可以认定服务器挂了。
至于保证流媒体服务器和Web服务器时间同步的问题,可以通过ntp来解决。
<?
include_once("../include/config.inc.php");
include_once("../include/mysql.inc.php");
$db = new SQL();
$db->Host = $dbHostName;
$db->Database = $dbDatabase;
$db->User = $dbUser;
$db->Password = $dbPassword;
$db->connect();
$nowtime=date("Y-m-d H:i:s");
$query_string = "select *,unix_timestamp('".$nowtime."')-unix_timestamp(InfoTime) as OverTime from ServerList";
$db->query($query_string);
echo "<html>";
echo "<head>";
echo '<meta http-equiv="Content-Type" content="text/html; charset=gb2312">';
echo '<title>服务器状态.</title>';
echo '<meta http-equiv="refresh" content="5">';
echo '</head>';
echo '<body>';
echo '<table align=center width="590" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="120"><div align="center">服务器名称</div></td>
<td width="120"><div align="center">服务器IP地址</div></td>
<td width="61"><div align="center">在线人数</div></td>
<td width="186"><div align="center">状态报告时间</div></td>
<td width="80"><div align="center">运行状态</div></td>
</tr>';
for(;;){
if($db->next_record()){
echo '<tr>';
echo '<td><div align="center">'.$db->f("ServerName").'</div></td>';
echo '<td><div align="center"><a href=http://.$db->f("ServerIP").':'.$db->f("AdminPort").'/admin/index.html">'.$db->f("ServerIP").'</a></div></td>';
echo '<td><div align="center">'.$db->f("ClientNum").'</div></td>';
echo '<td><div align="center">'.$db->f("InfoTime").'</div></td>';
$msg = "<font color=blue>正常运行</font>";
$intv = $db->f("OverTime");
if($inv>60){
$msg="<font color=red>挂了!!!</font>";
}
echo '<td><div align="center">'.$msg.'</div></td>';
echo '</tr>';
}
else
break;
}
echo '</table>
</body>
</html>';
exit(0);
4.设置Helix服务器中的Custom Logging
选中Server Stats这个模板,删除它的StdOut1输出方式,增加一个HTTPPOST方式
URL栏中填入以上程序的URL,记住不要"http://",系统会自动给你加上的。
Port填80
Output Interval 是服务器报告状态的时间间隔,我填的20秒。
Output Format输出格式
server_uptime=%Server.Uptime%&cpu_usage=%Server.PercentCPUUsage%&client_count=%Server.ClientCount%&bandwidth_output=%Server.BandwidthOutput%
%Server.Uptime%是Helix Server的变量,在输出时会被替换为当前的信息,还有很多变量的,你可以在Helix Server的文档中找到,因为我只需要知道这些信息就够了,你可以自己添加需要的信息。
注意:这里必须用=连接每个变量名和变量,用&连接所有的变量这是POST方式传递变量的方式,我没注意这一点,在这里花费了整整一天的时间
5.点Apply,大功告成!
Related posts:
- 闲谈 WordPress MU (四)
- Helix Universal Server 的几大不足
- 非常可爱的猫咪!^_^
- 原来错误信息也可以这么尿性
- 单机安装多个Helix Server服务
- Running IIS 32-bit Applications on IIS 64-bit
- DNS的备份和恢复
- 白羊座
- 射手座
- 用Sorenson来进行SDP直播发布
Tags: Database, Helix Server

