记录apache status状态信息自动更新到数据库的shell脚本

发布时间:2020-12-31编辑:脚本学堂
分享一例实用的shell脚本,功能为:记录apache status状态信息,并且自动更新到数据库中,有需要的朋友参考下。

1,获取apache status,monitor_log.sh
 

复制代码 代码示例:
#!/bin/bash 
 
#连接数 
site_connects=$(netstat -ant | grep $ip:80 | wc -l) 
#当前连接数 
site_cur_connects=$(netstat -ant | grep $ip:80 | grep EST | wc -l) 
 
#apache 
apache_speed=$(netstat -n | linuxjishu/13830.html target=_blank class=infotextkey>awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}') 
 
printf "[#start#]n$(date '+%Y-%m-%d %H:%M:%S')n" 
printf "connects:${site_connects}n" 
printf "cur connects:${site_cur_connects}n" 
printf "apache_speed:n${apache_speed}n[#end#]nn" 
 
exit 0 

设置任务计划,在终端设置crontab执行以上的shell/ target=_blank class=infotextkey>shell脚本
 

复制代码 代码示例:
* * * * * /home/fdipzone/monitor_log.sh >> /home/fdipzone/monitor.log 

2,将apache status log 写入数据库 save_monitor_log.php
 

复制代码 代码示例:
<?php 
$logfile = dirname(__FILE__).'/monitor.log'; 
$dbconfig = array( 
    'host' => '192.168.1.100', 
    'username' => 'username', 
    'password' => 'password', 
    'dbname' => 'mydb', 
    'tabname' => 'monitor_log' 
); 
 
$obj = new SaveMonitorLog($dbconfig, 'myweb'); 
$obj->load($logfile); 
 
// 读取monitor log,记录入db,查看db 
class SaveMonitorLog{ // class start 
    private $_apache_state = array('TIME_WAIT', 'CLOSE_WAIT', 'SYN_SENT', 'SYN_RECV', 'FIN_WAIT1', 'FIN_WAIT2', 'ESTABLISHED', 'LAST_ACK', 'CLOSING'); 
    private $_dbconfig = array(); 
    private $_site = null;
 
    /** init */ 
    public function __construct($dbconfig=array(), $site='web'){ 
        if(!isset($dbconfig['host']) || !isset($dbconfig['username']) || !isset($dbconfig['password']) || !isset($dbconfig['dbname']) || !isset($dbconfig['tabname'])){ 
    $this->debug('dbconfig error'); 
        } 
        $this->_dbconfig = $dbconfig; 
        $this->_site = $site; 
        $this->connectdb();   
    }
 
    /** load data
    * @param  String $logfile log文件
    * @return boolean
    */ 
    public function load($logfile){ 
 
        // 读取log数据 
        if(file_exists($logfile)){ 
    $logdata = file_get_contents($logfile); 
    // 清空monitor.log 
    file_put_contents($logfile, '', true); 
        }else{ 
    return false; 
        } 
 
        // 正則分析数据 [#start#]*[#end#] 
        preg_match_all('/[#start#](.*?)[#end#].*?/si', $logdata, $data); 
 
        if(isset($data[1]) && count($data[1])>0){ 
    $alldata = $data[1]; 
    foreach($alldata as $val){ 
        $indb = $this->parser($val); 
        $newid = $this->addtodb($indb); 
    } 
        } 
    } 
   
    /** parser data
    * @param  Array $data
    * @return Array
    */ 
    private function parser($data){ 
        $indb = array(); 
        $tmp = explode(chr(10), $data); // 按換行分隔 
 
        $indb['site'] = $this->_site; 
        $indb['addtime'] = $tmp[1]; 
        $indb['connects'] = array_pop(explode(':',$tmp[2])); 
        $indb['cur_connects'] = array_pop(explode(':',$tmp[3])); 
 
        for($i=5, $max=count($tmp)-2; $i<$max; $i++){ 
    list($key, $num) = explode(' ', $tmp[$i]); 
    if(in_array($key, $this->_apache_state)){ 
        $indb[$key] = $num; 
    } 
        } 
        return $indb; 
    }
 
    /** connect db */ 
    private function connectdb(){ 
        $conn=@mysql_connect($this->_dbconfig['host'], $this->_dbconfig['username'], $this->_dbconfig['password'])  or die(mysql_error()); 
        mysql_select_db($this->_dbconfig['dbname'], $conn) or die(mysql_error()); 
    }
 
    /** add to db */ 
    private function addtodb($indb){ 
        $insertkey = ''; 
        $insertval = ''; 
        if($indb){ 
    foreach($indb as $key=>$val){ 
        $insertkey .= $insertkey? " ,".$key : $key; 
        $insertval .= $insertval? " ,'".mysql_escape_string(trim($val))."'" : "'".mysql_escape_string(trim($val))."'"; 
    } 
    $sqlstr = "insert into ".$this->_dbconfig['tabname']."($insertkey) values($insertval)"; 
    $query = @mysql_query($sqlstr) or die(mysql_error()); 
    $id = mysql_insert_id(); 
    return $id? $id : false; 
        } 
    }
 
    /** debug */ 
    private function debug($msg){ 
        exit($msg."rn"); 
    }
} // class end
?> 

设置计划任务,在终端crontab执行:
 

复制代码 代码示例:
0 0 * * * php /home/fdipzone/save_monitor_log.php 

3,表table:monitor_log struct
 

复制代码 代码示例:
CREATE TABLE IF NOT EXISTS `monitor_log` ( 
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
  `site` varchar(20) NOT NULL, 
  `connects` int(10) unsigned NOT NULL DEFAULT '0', 
  `cur_connects` int(10) unsigned NOT NULL DEFAULT '0', 
  `TIME_WAIT` int(10) unsigned NOT NULL DEFAULT '0', 
  `CLOSE_WAIT` int(10) unsigned NOT NULL DEFAULT '0', 
  `SYN_SENT` int(10) unsigned NOT NULL DEFAULT '0', 
  `SYN_RECV` int(10) unsigned NOT NULL DEFAULT '0', 
  `FIN_WAIT1` int(10) unsigned NOT NULL DEFAULT '0', 
  `FIN_WAIT2` int(10) unsigned NOT NULL DEFAULT '0', 
  `ESTABLISHED` int(10) unsigned NOT NULL DEFAULT '0', 
  `LAST_ACK` int(10) unsigned NOT NULL DEFAULT '0', 
  `CLOSING` int(10) unsigned NOT NULL DEFAULT '0', 
  `addtime` datetime NOT NULL, 
  PRIMARY KEY (`id`), 
  KEY `connects` (`connects`), 
  KEY `cur_connects` (`cur_connects`), 
  KEY `TIME_WAIT` (`TIME_WAIT`), 
  KEY `CLOSE_WAIT` (`CLOSE_WAIT`), 
  KEY `SYN_SENT` (`SYN_SENT`), 
  KEY `SYN_RECV` (`SYN_RECV`), 
  KEY `FIN_WAIT1` (`FIN_WAIT1`), 
  KEY `FIN_WAIT2` (`FIN_WAIT2`), 
  KEY `ESTABLISHED` (`ESTABLISHED`), 
  KEY `LAST_ACK` (`LAST_ACK`), 
  KEY `CLOSING` (`CLOSING`), 
  KEY `addtime` (`addtime`) 
) ENGINE=myisam  DEFAULT CHARSET=latin1 ; 

附,查看当前Apache的连接数的方法:
查看Apache当前连接数、IP访问数的方法
Linux下查看apache进程数、连接数的命令