前文:
1週間前、ローカルファイルサーバー、ルーター、ロッキングチェアなど、デスクトップマシンを維持するのにうんざりしている便利なサービスを整理するために安価なPCを購入しました。 このすべてがgentooを中心に展開しています。
アンブラ:
今日は夕方と少し改造されたwTorrentを過ごしました。
これは次のようになります。
 
      イノベーションから:
1.これで、トラフィックの合計量が表示されます。
2.ページの中央に表示される着信/発信トラフィックのグラフ。
最初の点を考慮してください。 その実装は簡単です:
rtorrentスケジューラーにルールを追加します。 これは、クライアントでctrl + xを使用するか、構成に行を追加して再起動することにより行われます。
schedule = export_traffic、0.15、「execute = / var / www / localhost / htdocs / wtorrent / schedule / passthrough、$ get_up_total =、$ get_down_total =、/ var / www / localhost / htdocs / wtorrent / schedule / traffic.rtorrent 」
小さなパススルースクリプトを介してtraffic.rtorrentファイルにこのルールを15秒ごとに
#!/bin/sh 
      
        
        
        
      
     echo $1 $2 > $3 
      
        
        
        
      
    
      
      発信と着信のトラフィック量の2つの数値を書き込みます。 これは、xmlrpcを介して符号付き整数が与えられ、トラフィック量が2GBを超えると負の範囲に入ることが予想されるためです。:-)私は彼に大きな数を与える方法を教える方法を見つけられませんでした。
rtorrent.cls.phpに次の3つのメソッドを追加します。
private function getTrafficInfo() 
      
        
        
        
      
     { 
      
        
        
        
      
     static $data; 
      
        
        
        
      
     
      
        
        
        
      
     if (!$data){ 
      
        
        
        
      
     if (is_file('./schedule/traffic.rtorrent')) { 
      
        
        
        
      
     $data = file('./schedule/traffic.rtorrent'); 
      
        
        
        
      
     $data = explode(' ', $data[0]); 
      
        
        
        
      
     } else { 
      
        
        
        
      
     $data = array(0, 0); 
      
        
        
        
      
     } 
      
        
        
        
      
     } 
      
        
        
        
      
     
      
        
        
        
      
     return $data; 
      
        
        
        
      
     } 
      
        
        
        
      
     
      
        
        
        
      
     public function getDownTotal() 
      
        
        
        
      
     { 
      
        
        
        
      
     if ($data = $this->getTrafficInfo()) { 
      
        
        
        
      
     return $this->getCorrectUnits($data[1]); 
      
        
        
        
      
     } 
      
        
        
        
      
     
      
        
        
        
      
     return $this->getCorrectUnits($this->get_info_rtorrent('get_down_total', false, false)); 
      
        
        
        
      
     } 
      
        
        
        
      
     
      
        
        
        
      
     public function getUpTotal() 
      
        
        
        
      
     { 
      
        
        
        
      
     if ($data = $this->getTrafficInfo()) { 
      
        
        
        
      
     return $this->getCorrectUnits($data[0]); 
      
        
        
        
      
     } 
      
        
        
        
      
     
      
        
        
        
      
     return $this->getCorrectUnits($this->get_info_rtorrent('get_up_total', false, false)); 
      
        
        
        
      
     } 
      
        
        
        
      
    
      
      トレントクライアントによってエクスポートされた番号をファイルから抽出するだけです。 さて、menu.tpl.phpテンプレートで、これらの2つの数字を表示するだけです:
{$str.dw_rate} {$web->getDownload()} ({$web->getDownTotal()}) 
      
        
        
        
      
     {$str.up_rate} {$web->getUpload()} ({$web->getUpTotal()}) 
      
        
        
        
      
    
      
      現在の速度でブロックの後にブラケットに追加します。
グラフィックを描くタスクはもう少し複雑です。
それを実装するために、スケジューラにもう1つのルールを追加します。
schedule = import_traffic、0.60、「execute = / usr / bin / php、-f、/ var / www / localhost / htdocs / wtorrent / schedule / bandwidth.php」
このルールは毎分phpスクリプトを実行します:
<?php 
      
        
        
        
      
     
      
        
        
        
      
     chdir(dirname(__FILE__)); 
      
        
        
        
      
     
      
        
        
        
      
     $datafile = './traffic.rtorrent'; 
      
        
        
        
      
     
      
        
        
        
      
     if (is_file($datafile)) { 
      
        
        
        
      
     $data = file($datafile); 
      
        
        
        
      
     list($out, $in) = explode(' ', $data[0]); 
      
        
        
        
      
     
      
        
        
        
      
     $out = (double)$out; 
      
        
        
        
      
     $in = (double)$in; 
      
        
        
        
      
     
      
        
        
        
      
     require_once '../conf/user.conf.php'; 
      
        
        
        
      
     
      
        
        
        
      
     $db = new PDO(DB_STAT_DSN, DB_STAT_LOGIN, DB_STAT_PWD); 
      
        
        
        
      
     
      
        
        
        
      
     $qry = 'SELECT `in`, `out`, `time` FROM `bandwidth` ORDER BY `id` DESC LIMIT 1'; 
      
        
        
        
      
     $stmt = $db->query($qry); 
      
        
        
        
      
     
      
        
        
        
      
     $delta_in = 0; 
      
        
        
        
      
     $delta_out = 0; 
      
        
        
        
      
     
      
        
        
        
      
     if ($row = $stmt->fetch()) { 
      
        
        
        
      
     if ($in >= $row['in'] && $out >= $row['out']) { 
      
        
        
        
      
     $delta_in = $in - $row['in']; 
      
        
        
        
      
     $delta_out = $out - $row['out']; 
      
        
        
        
      
     } 
      
        
        
        
      
     } 
      
        
        
        
      
     
      
        
        
        
      
     $db->query('INSERT INTO `bandwidth` (`in`, `delta_in`, `out`, `delta_out`, `period`) VALUES (' . $in . ', ' . $delta_in . ', ' . $out . ', ' . $delta_out . ", TIME_TO_SEC(TIMEDIFF(NOW(), '" . $row['time'] . "')))"); 
      
        
        
        
      
     } 
      
        
        
        
      
     
      
        
        
        
      
     ?>
      
      このプリミティブスクリプトは、traffic.torrentファイル内の現在のトラフィック値を比較し、このデータに基づいて、現在の値と差分をデータベース内の以前の値に書き込みます。
以下は、データベースのスキーマと、スクリプトがデータを書き込むテーブルです。
CREATE DATABASE `rtorrent` 
      
        
        
        
      
     CHARACTER SET 'utf8' 
      
        
        
        
      
     COLLATE 'utf8_general_ci'; 
      
        
        
        
      
     
      
        
        
        
      
     CREATE TABLE `bandwidth` ( 
      
        
        
        
      
     `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, 
      
        
        
        
      
     `in` BIGINT(20) DEFAULT NULL, 
      
        
        
        
      
     `delta_in` BIGINT(20) DEFAULT NULL, 
      
        
        
        
      
     `out` BIGINT(20) DEFAULT NULL, 
      
        
        
        
      
     `delta_out` BIGINT(20) DEFAULT NULL, 
      
        
        
        
      
     `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
      
        
        
        
      
     `period` INTEGER(11) DEFAULT NULL, 
      
        
        
        
      
     PRIMARY KEY (`id`), 
      
        
        
        
      
     KEY `time` (`time`) 
      
        
        
        
      
     )ENGINE=MyISAM 
      
        
        
        
      
     AUTO_INCREMENT=1 ROW_FORMAT=FIXED CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'; 
      
        
        
        
      
    
      
      少し残っています:-)グラフを描くスクリプトはjpgraphを使用します 。 スクリプト自体の内容は次のとおりです。
<?php 
      
        
        
        
      
     
      
        
        
        
      
     include "../../lib/jpgraph/jpgraph.php"; 
      
        
        
        
      
     include "../../lib/jpgraph/jpgraph_line.php"; 
      
        
        
        
      
     include "../../lib/jpgraph/jpgraph_scatter.php"; 
      
        
        
        
      
     include "../../lib/jpgraph/jpgraph_regstat.php"; 
      
        
        
        
      
     
      
        
        
        
      
     include "../../conf/user.conf.php"; 
      
        
        
        
      
     
      
        
        
        
      
     $db = new PDO(DB_STAT_DSN, DB_STAT_LOGIN, DB_STAT_PWD); 
      
        
        
        
      
     
      
        
        
        
      
     $stmt = $db->query('SELECT SUM(`delta_in`) / 1024 / SUM(`period`) AS `in`, 
      
        
        
        
      
     SUM(`delta_out`) / 1024 / SUM(`period`) AS `out`, 
      
        
        
        
      
     MAX(`time`) AS `time` 
      
        
        
        
      
     FROM `bandwidth` 
      
        
        
        
      
     WHERE `time` > DATE_SUB(NOW(), INTERVAL 12 HOUR) 
      
        
        
        
      
     GROUP BY CONCAT(DATE(`time`), HOUR(`time`), FLOOR(MINUTE(`time`) / 5)) 
      
        
        
        
      
     ORDER BY `time`'); 
      
        
        
        
      
     $x = $in = $out = array(); 
      
        
        
        
      
     while ($row = $stmt->fetch()) { 
      
        
        
        
      
     $ts = strtotime($row['time']); 
      
        
        
        
      
     $x[] = $ts; 
      
        
        
        
      
     $in[] = $row['in']; 
      
        
        
        
      
     $out[] = $row['out']; 
      
        
        
        
      
     } 
      
        
        
        
      
     
      
        
        
        
      
     // Setup the basic graph 
      
        
        
        
      
     $graph = new Graph(800, 400); 
      
        
        
        
      
     $graph->SetMargin(30, 10, 0, 30); 
      
        
        
        
      
     $graph->title->Set('Bandwidth, KB/s'); 
      
        
        
        
      
     $graph->SetAlphaBlending(); 
      
        
        
        
      
     
      
        
        
        
      
     // Setup a manual x-scale (We leave the sentinels for the 
      
        
        
        
      
     // Y-axis at 0 which will then autoscale the Y-axis.) 
      
        
        
        
      
     // We could also use autoscaling for the x-axis but then it 
      
        
        
        
      
     // probably will start a little bit earlier than the first value 
      
        
        
        
      
     // to make the first value an even number as it sees the timestamp 
      
        
        
        
      
     // as an normal integer value. 
      
        
        
        
      
     $graph->SetScale("intlin", 0, max(max($out), max($in)) * 1.1, reset($x), end($x)); 
      
        
        
        
      
     $graph->xgrid->Show(); 
      
        
        
        
      
     $graph->yaxis->HideZeroLabel(); 
      
        
        
        
      
     $graph->SetFrame(false); 
      
        
        
        
      
     
      
        
        
        
      
     function TimeCallback($aVal) { 
      
        
        
        
      
     return Date('H:i',$aVal); 
      
        
        
        
      
     } 
      
        
        
        
      
     // Setup the x-axis with a format callback to convert the timestamp 
      
        
        
        
      
     // to a user readable time 
      
        
        
        
      
     $graph->xaxis->SetLabelFormatCallback('TimeCallback'); 
      
        
        
        
      
     //$graph->xaxis->SetLabelAngle(90); 
      
        
        
        
      
     
      
        
        
        
      
     // Create the line 
      
        
        
        
      
     $p1 = new LinePlot($out, $x); 
      
        
        
        
      
     $p1->SetColor("red"); 
      
        
        
        
      
     
      
        
        
        
      
     $p2 = new LinePlot($in, $x); 
      
        
        
        
      
     $p2->SetColor("blue"); 
      
        
        
        
      
     
      
        
        
        
      
     // Add lineplot to the graph 
      
        
        
        
      
     $graph->Add($p1); 
      
        
        
        
      
     $graph->Add($p2); 
      
        
        
        
      
     
      
        
        
        
      
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
      
        
        
        
      
     header("Last-Modified: " . gmdate("D, d MYH:i:s") . " GMT"); 
      
        
        
        
      
     header("Cache-Control: no-cache, must-revalidate"); 
      
        
        
        
      
     header("Cache-Control: post-check=0,pre-check=0"); 
      
        
        
        
      
     header("Cache-Control: max-age=0"); 
      
        
        
        
      
     header("Pragma: no-cache"); 
      
        
        
        
      
     
      
        
        
        
      
     // Output line 
      
        
        
        
      
     $graph->Stroke(); 
      
        
        
        
      
     
      
        
        
        
      
     ?>
      
      まず、必要なライブラリを接続します。 次に-簡単なリクエストで、データベースからデータを選択します。 さらに、スケジュールをより平均化してスムーズにするために、5分間隔で集計します。 次に、jpgraphに付属する例に基づいて、目的の形式のグラフを「収集」します。 結果のコードはファイルwt / img / bandwidth.phpに配置されます。
最後に、結果のグラフをwtorrentページに表示します。
メニュー項目を追加します。 これは、menu_adminメニューを使用して連想配列に 'Graphic' => 'Graphic'要素を追加することにより、rtorrent.cls.phpファイルでも実行されます。
次に-Graphic.cls.phpページの表示を制御するクラスを作成します。
<?php 
      
        
        
        
      
     
      
        
        
        
      
     class Graphic extends rtorrent 
      
        
        
        
      
     { 
      
        
        
        
      
     public function construct() 
      
        
        
        
      
     { 
      
        
        
        
      
     if(!$this->setClient()) 
      
        
        
        
      
     { 
      
        
        
        
      
     return false; 
      
        
        
        
      
     } 
      
        
        
        
      
     } 
      
        
        
        
      
     } 
      
        
        
        
      
     ?> 
      
        
        
        
      
    
      
      最後に、グラフであるgraphic.tpl.phpを表示する1行のテンプレートを作成します。

      
      これらすべての簡単な操作の後、上記で示した結果と同様の結果が得られるはずです。 :-)
PS:上記のすべてのソースコードは、 ここからダウンロードできます 。