使用PHP获取UNIX服务器每月的带宽使用情况
我是否可以使用php获取服务器每月的带宽使用情况?使用PHP获取UNIX服务器每月的带宽使用情况,php,linux,unix,Php,Linux,Unix,我是否可以使用php获取服务器每月的带宽使用情况? 谢谢。您的最佳选择是解析通过ethX inteface的数据包数 跟踪传递的字节的命令是/sbin/ifconfig 请记住,如果重新启动linux机箱,计数器将重置 eth0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:32363649 errors:0 dropped:0 overruns:0 frame:0
谢谢。您的最佳选择是解析通过ethX inteface的数据包数 跟踪传递的字节的命令是/sbin/ifconfig 请记住,如果重新启动linux机箱,计数器将重置
eth0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:32363649 errors:0 dropped:0 overruns:0 frame:0
TX packets:35133219 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2813232645 (2.6 GiB) TX bytes:1696525681 (1.5 GiB)
Interrupt:16 Memory:f4000000-f4012700
您可以解析站点的apache访问日志以计算总带宽。下面是一个松散的伪php示例(实际实现将取决于您的日志格式):
重新启动将是一个问题,无效,肯定会有错误!如果读取的值小于之前的值,则表示发生了重新启动,然后只需将其添加到上次保存的值+一些错误余量(这将是平均每小时带宽*自上次检查以来的小时数…需要一些锻炼,但这是一个好主意…有没有办法通过windows实现它?这里有很长时间的linux系统管理员,很抱歉没有windows解决方案:)我想Windows也会记录一些流量计数器,毕竟它显示在网络接口状态下,在这种情况下,考虑设置AWSTATS或类似的软件,这是为了日志文件分析/统计,使它保持自己定期更新(通过CRONJOBE),然后解析AWSTATS的数据缓存文件。
<?php
$logfile = '/var/log/apache/httpd-access.log';
$startDate = '2010-10-01';
$endDate = '2010-10-31';
$fh = fopen($logfile, 'r');
if (!$fh) die('Couldn\'t open log file.');
$totalBytes = 0;
// let's pretend the log is a csv file because i'm lazy at parsing
while (($info = fgetcsv($fh, 0, ' ', '"')) !== false) {
// get the date of the log entry
$date = $info[3];
// check if the date is within our month of interest
if ($date > $startDate && $date < $endDate) {
// get the number of bytes sent in the request
$totalBytes += $info[7];
}
}
fclose($fh);
echo 'Total bytes used: ' . $totalBytes;