SYSLOG検索くん 第6回

今回はPHPでMySQLからの検索して結果を表示する画面を作成します。
ひとまず説明は後でファイルを作成します。
ファイル名は「syslog.php」になります。

syslog.php
<?php
/* セッション作成 */
session_start();

/* 年月日判定 */
if(isset($_POST[‘send’])) {
$years = $_POST[‘years’];
$months = $_POST[‘months’];
$days = $_POST[‘days’];
$yeare = $_POST[‘yeare’];
$monthe = $_POST[‘monthe’];
$daye = $_POST[‘daye’];
} else {
$years = date(“Y”,strtotime(“-1 day”));
$months = date(“m”,strtotime(“-1 day”));
$days = date(“d”,strtotime(“-1 day”));
$yeare = date(“Y”);
$monthe = date(“m”);
$daye = date(“d”);
}

/* ページ表示判定 */
if(isset($_GET[‘page’]) == false){
$page = 0;
$node = $_POST[‘node’];
$moji = $_POST[‘moji’];
} else {
/* セッションからの展開 */
$page = $_GET[‘page’];
$node = $_SESSION[‘node’];
$moji = $_SESSION[‘moji’];
$years = $_SESSION[‘years’];
$months = $_SESSION[‘months’];
$days = $_SESSION[‘days’];
$yeare = $_SESSION[‘yeare’];
$monthe = $_SESSION[‘monthe’];
$daye = $_SESSION[‘daye’];
}

/* 維持セッション登録 */
$_SESSION[‘node’] = $node;
$_SESSION[‘moji’] = $moji;
$_SESSION[‘years’] = $years;
$_SESSION[‘months’] = $months;
$_SESSION[‘days’] = $days;
$_SESSION[‘yeare’] = $yeare;
$_SESSION[‘monthe’] = $monthe;
$_SESSION[‘daye’] = $daye;

/* 固定値 */
$page_rows = 100;
$last_rows = $page + $page_rows -1;

/* セレクトオプションのループ設定 */
function optionLoop($start, $end, $value = null){

for($i = $start; $i <= $end; $i++){
if(isset($value) && $value == $i){
echo “<option value=\”{$i}\” selected=\”selected\”>{$i}</option>”;
}else{
echo “<option value=\”{$i}\”>{$i}</option>”;
}
}
}

?>
<?php include(“header.html”); ?>
<h2>SYSLOG抽出</h2>
<div style=”float:left;” id=”nav”>
<form name=”node” action=”cisco.php” method=”post”>
<b>抽出期間</b><br>

<select name=”years”>
<?php optionLoop(‘2012’, date(“Y”), $years);?>
</select> 年 

<select name=”months”>
<?php optionLoop(‘1′, ’12’, $months);?>
</select> 月 

<select name=”days”>
<?php optionLoop(‘1′, ’31’, $days);?>
</select> 日 
~ 

<select name=”yeare”>
<?php optionLoop(‘2012’, date(“Y”), $yeare);?>
</select> 年 
<select name=”monthe”>
<?php optionLoop(‘1′, ’12’, $monthe);?>
</select> 月 
<select name=”daye”>
<?php optionLoop(‘1′, ’31’, $daye);?>
</select> 日 
<br>
<b>ノード名</b>
<input style=”background-color:#CECFCE;” type=”text” name=”node” id=”node” value=”<?php print(htmlspecialchars($_SESSION[‘node’], ENT_QUOTES)); ?>” > 
<b>文字列</b>
<input style=”background-color:#CECFCE;” type=”text” name=”moji” id=”moji” value=”<?php print(htmlspecialchars($_SESSION[‘moji’], ENT_QUOTES)); ?>” > <input type=”submit” name=”send” value=”検索”><br>
</form>
</div>
<div id=”contents”>
<?php
/* ログイン */
$link = mysql_connect(‘localhost’, ‘admin’, ‘password’);
if (!$link) {
die(‘接続失敗です。’.mysql_error());
}

/* データベース選択 */
$db_selected = mysql_select_db(‘syslogdb’, $link);
if (!$db_selected){
die(‘データベース選択失敗です。’.mysql_error());
}

mysql_set_charset(‘utf8’);

/*テーブルヘッダを出力する。 */
print( “<table>” );
print( “<tr>” );
print( “<th>年月日</th>” );
print( “<th>時間</th>” );
print( “<th>ノード名</th>” );
print( “<th>ログ情報</th>” );
print( “</tr>” );

/* データ検索条件 */

/* 検索用2桁登録 */
$months = sprintf(“%02d”,$months);
$days = sprintf(“%02d”,$days);
$monthe = sprintf(“%02d”,$monthe);
$daye = sprintf(“%02d”,$daye);

/* エラー判定 */
if(isset($_POST[‘send’]) == false && isset($_GET[‘page’]) == false){
print(“</table>”);
} else if ($years > $yeare){
print(“</table>”);
echo 抽出年に誤りがあります。;
echo “$years$months$days,$yeare$monthe$daye”;
} else if ($years >= $yeare && $months > $monthe){
print(“</table>”);
echo 抽出月に誤りがあります。;
echo “$years$months$days,$yeare$monthe$daye”;
} else if ($years >= $yeare && $months >= $monthe && $days > $daye){
print(“</table>”);
echo 抽出日に誤りがあります。;
echo “$years$months$days,$yeare$monthe$daye”;
} else {

/* 該当テーブルの行数を取得 */
$select_all = “select * from syslogtable where date between ‘$years$months$days’ and ‘$yeare$monthe$daye’ and node like ‘%$node%’ and log like ‘%$moji%'”;
$result_all = mysql_query($select_all);
$all_rows = mysql_num_rows($result_all);
/* SQL実行 */
$select = “select * from syslogtable where date between ‘$years$months$days’ and ‘$yeare$monthe$daye’ and node like ‘%$node%’ and log like ‘%$moji%’ limit $page,$page_rows”;
$result = mysql_query($select);
if (!$result) {
die(‘1クエリーが失敗しました。’.mysql_error());
}

while ($row = mysql_fetch_assoc($result)){
print(“<tr>”);
print(“<td>”.$row[‘date’].”</td>”);
print(“<td>”.$row[‘time’].”</td>”);
print(“<td>”.$row[‘node’].”</td>”);
print(“<td>”.$row[‘log’].”</td>”);
print(‘</tr>’);
}
print(“</table>”);

//ページ表示
if ($all_rows < 100) {
print(“1ページ中1ページ目を表示<br>”);
} else {
print(ceil($all_rows/100).”ページ中”.ceil($page/100 + 1).”ページ目を表示<br>”);
}

//前の100件
if ($page > 0) {
print(“<a href = cisco.php?page=”.($page – $page_rows).”>&lt 前の100件</a>     ”);
}

//次の100件
if ( $last_rows < ($all_rows-1)) {
print (“<a href = cisco.php?page=”.($page + $page_rows).”>次の100件 &gt</a>”);
}
print (“<br><a href = csv.php?csv=syslog>csv出力する</a>”);
}

?>
</div>
<!– 回り込み解除 –>
<div style=”clear:both;”></div>

<?php include(“footer.html”); ?>

ざっと全部書きましたが機能要件を満たす形で作成しています。
要件から追加した機能としては100件ずつ表示させるようにしています。
あんまりいっぺんに表示されても大変ですから・・・
当然100件以上のログが見れないのは困りますから次の100件へのリンクと
戻るリンクを作成しています。
年月日のプルダウンは普通に数字を入れてもいいのですがコードが長くなるのでループ設定をしています。
あとPHPの部分とHTMLの部分でコメント判定が変わってくるので注意して下さい。
PHP → /* 中が全部コメント */
    // この行はコメント

HTML → <!– 中が全部コメント –>

また抽出の為にcsv.phpというファイルにて出力させるようにしています。
csv.phpについては次回説明します。

今回はPHPでの集計部分をやりました。次回は当日ログの取り込み部分です。

コメントする