serverStats.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /* $Id: serverStats.php 3010 2007-05-17 20:23:53Z msn_exploder $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. // prevent direct invocation
  16. if ((!isset($cfg['user'])) || (isset($_REQUEST['cfg']))) {
  17. @ob_end_clean();
  18. @header("location: ../../index.php");
  19. exit();
  20. }
  21. /******************************************************************************/
  22. // default-type
  23. define('_DEFAULT_TYPE', 'all');
  24. // init template-instance
  25. tmplInitializeInstance($cfg["theme"], "page.serverStats.tmpl");
  26. // request-vars
  27. $type = (isset($_REQUEST['type'])) ? tfb_getRequestVar('type') : _DEFAULT_TYPE;
  28. // types
  29. $type_list = array();
  30. array_push($type_list, array(
  31. 'name' => "all",
  32. 'selected' => ($type == "all") ? 1 : 0
  33. )
  34. );
  35. array_push($type_list, array(
  36. 'name' => "drivespace",
  37. 'selected' => ($type == "drivespace") ? 1 : 0
  38. )
  39. );
  40. array_push($type_list, array(
  41. 'name' => "who",
  42. 'selected' => ($type == "who") ? 1 : 0
  43. )
  44. );
  45. if ($cfg['isAdmin'] == 1)
  46. array_push($type_list, array(
  47. 'name' => "ps",
  48. 'selected' => ($type == "ps") ? 1 : 0
  49. )
  50. );
  51. if ($cfg['isAdmin'] == 1)
  52. array_push($type_list, array(
  53. 'name' => "netstat",
  54. 'selected' => ($type == "netstat") ? 1 : 0
  55. )
  56. );
  57. if (($cfg['enable_xfer'] == 1) && (($cfg['enable_public_xfer'] == 1 ) || $cfg['isAdmin']))
  58. array_push($type_list, array(
  59. 'name' => "xfer",
  60. 'selected' => ($type == "xfer") ? 1 : 0
  61. )
  62. );
  63. $tmpl->setloop('type_list', $type_list);
  64. // type-switch
  65. switch ($type) {
  66. // all
  67. case "all":
  68. // set vars
  69. $tmpl->setvar('all_df', shell_exec("df -h ".tfb_shellencode($cfg["path"])));
  70. $tmpl->setvar('all_du', shell_exec("du -sh ".tfb_shellencode($cfg["path"]."*")));
  71. $tmpl->setvar('all_w', shell_exec("w"));
  72. $tmpl->setvar('all_free', shell_exec("free -mo"));
  73. // language
  74. $tmpl->setvar('_DRIVESPACE', $cfg['_DRIVESPACE']);
  75. $tmpl->setvar('_SERVERSTATS', $cfg['_SERVERSTATS']);
  76. // drivespace-bar
  77. tmplSetDriveSpaceBar();
  78. break;
  79. // drivespace
  80. case "drivespace":
  81. // set vars
  82. $tmpl->setvar('drivespace_df', shell_exec("df -h ".tfb_shellencode($cfg["path"])));
  83. $tmpl->setvar('drivespace_du', shell_exec("du -sh ".tfb_shellencode($cfg["path"]."*")));
  84. // language
  85. tmplSetTitleBar($cfg['_DRIVESPACE']);
  86. // drivespace-bar
  87. tmplSetDriveSpaceBar();
  88. break;
  89. // who
  90. case "who":
  91. // set vars
  92. $tmpl->setvar('who_w', shell_exec("w"));
  93. $tmpl->setvar('who_free', shell_exec("free -mo"));
  94. // drivespace-bar
  95. tmplSetDriveSpaceBar();
  96. break;
  97. // ps
  98. case "ps":
  99. // set vars
  100. if ($cfg['isAdmin']) {
  101. // array with all clients
  102. $clients = array('tornado', 'transmission', 'mainline', 'wget', 'nzbperl', 'azureus');
  103. // get informations
  104. $process_list = array();
  105. foreach ($clients as $client) {
  106. $ch = ClientHandler::getInstance($client);
  107. array_push($process_list, array(
  108. 'client' => $client,
  109. 'RunningProcessInfo' => $ch->runningProcessInfo(),
  110. 'pinfo' => shell_exec("ps auxww | ".$cfg['bin_grep']." ".tfb_shellencode($ch->binClient)." | ".$cfg['bin_grep']." -v grep")
  111. )
  112. );
  113. }
  114. $tmpl->setloop('process_list', $process_list);
  115. }
  116. // drivespace-bar
  117. tmplSetDriveSpaceBar();
  118. break;
  119. // netstat
  120. case "netstat":
  121. // set vars
  122. if ($cfg['isAdmin']) {
  123. // set vars
  124. $tmpl->setvar('netstatConnectionsSum', netstatConnectionsSum());
  125. $tmpl->setvar('netstatPortList', netstatPortList());
  126. $tmpl->setvar('netstatHostList', netstatHostList());
  127. }
  128. // language
  129. $tmpl->setvar('_ID_HOSTS', $cfg['_ID_HOSTS']);
  130. $tmpl->setvar('_ID_PORTS', $cfg['_ID_PORTS']);
  131. $tmpl->setvar('_ID_CONNECTIONS', $cfg['_ID_CONNECTIONS']);
  132. // drivespace-bar
  133. tmplSetDriveSpaceBar();
  134. break;
  135. // xfer
  136. case "xfer":
  137. // is enabled ?
  138. if ($cfg["enable_xfer"] != 1) {
  139. AuditAction($cfg["constants"]["error"], "ILLEGAL ACCESS: ".$cfg["user"]." tried to use xfer");
  140. @error("xfer is disabled", "index.php?iid=serverStats", "");
  141. }
  142. // set vars
  143. $tmpl->setvar('is_xfer', 1);
  144. // getTransferListArray to update xfer-stats
  145. // set xfer-realtime
  146. $cfg['xfer_realtime'] = 1;
  147. // set xfer-newday
  148. Xfer::setNewday();
  149. // transferlist-array to update stats
  150. getTransferListArray();
  151. // xfer-totals
  152. $xfer_total = Xfer::getStatsTotal();
  153. if ($cfg['xfer_day'])
  154. $tmpl->setvar('xfer_day', tmplGetXferBar($cfg['xfer_day'],$xfer_total['day']['total'],$cfg['_XFERTHRU'].' Today:'));
  155. if ($cfg['xfer_week'])
  156. $tmpl->setvar('xfer_week', tmplGetXferBar($cfg['xfer_week'],$xfer_total['week']['total'],$cfg['_XFERTHRU'].' '.$cfg['week_start'].':'));
  157. $monthStart = strtotime(date('Y-m-').$cfg['month_start']);
  158. $monthText = (date('j') < $cfg['month_start']) ? date('M j',strtotime('-1 Day',$monthStart)) : date('M j',strtotime('+1 Month -1 Day',$monthStart));
  159. if ($cfg['xfer_month'])
  160. $tmpl->setvar('xfer_month', tmplGetXferBar($cfg['xfer_month'],$xfer_total['month']['total'],$cfg['_XFERTHRU'].' '.$monthText.':'));
  161. if ($cfg['xfer_total'])
  162. $tmpl->setvar('xfer_total', tmplGetXferBar($cfg['xfer_total'],$xfer_total['total']['total'],$cfg['_TOTALXFER'].':'));
  163. if (($cfg['enable_public_xfer'] == 1 ) || $cfg['isAdmin']) {
  164. $tmpl->setvar('show_xfer', 1);
  165. $sql = 'SELECT user_id FROM tf_users ORDER BY user_id';
  166. $rtnValue = $db->GetCol($sql);
  167. if ($db->ErrorNo() != 0) dbError($sql);
  168. $xfer = Xfer::getStats();
  169. $user_list = array();
  170. foreach ($rtnValue as $user_id) {
  171. array_push($user_list, array(
  172. 'user_id' => $user_id,
  173. 'total' => formatFreeSpace(@ $xfer["$user_id"]['total']['total'] / (1048576)),
  174. 'month' => formatFreeSpace(@ $xfer["$user_id"]['month']['total'] / (1048576)),
  175. 'week' => formatFreeSpace(@ $xfer["$user_id"]['week']['total'] / (1048576)),
  176. 'day' => formatFreeSpace(@ $xfer["$user_id"]['day']['total'] / (1048576))
  177. )
  178. );
  179. }
  180. $tmpl->setloop('user_list', $user_list);
  181. $tmpl->setvar('total_total', formatFreeSpace(@ $xfer_total['total']['total'] / (1048576)));
  182. $tmpl->setvar('total_month', formatFreeSpace(@ $xfer_total['month']['total'] / (1048576)));
  183. $tmpl->setvar('total_week', formatFreeSpace(@ $xfer_total['week']['total'] / (1048576)));
  184. $tmpl->setvar('total_day', formatFreeSpace(@ $xfer_total['day']['total'] / (1048576)));
  185. //
  186. $username = tfb_getRequestVar('user');
  187. $tmpl->setvar('user', $username);
  188. $_month = tfb_getRequestVar('month');
  189. if (isset($_REQUEST['month'])) {
  190. $mstart = $_month.'-'.$cfg['month_start'];
  191. $mend = date('Y-m-d', strtotime('+1 Month', strtotime($mstart)));
  192. } else {
  193. $mstart = 0;
  194. $mend = 0;
  195. }
  196. $_week = tfb_getRequestVar('week');
  197. if (isset($_REQUEST['week'])) {
  198. $wstart = $_week;
  199. $wend = date('Y-m-d', strtotime('+1 Week', strtotime($wstart)));
  200. } else {
  201. $wstart = $mstart;
  202. $wend = $mend;
  203. }
  204. // month stats
  205. $xferStats = Xfer::getUsageByDate($username);
  206. $start = '';
  207. $download = 0;
  208. $upload = 0;
  209. $month_list = array();
  210. foreach ($xferStats as $row) {
  211. $rtime = strtotime($row[2]);
  212. $newstart = $cfg['month_start'].' ';
  213. $newstart .= (date('j',$rtime) < $cfg['month_start']) ? date('M Y',strtotime('-1 Month',$rtime)) : date('M Y',$rtime);
  214. if ($start != $newstart) {
  215. if ($upload + $download != 0) {
  216. array_push($month_list, array(
  217. 'user_id' => $username,
  218. 'month' => date('Y-m',strtotime($start)),
  219. 'start' => $start,
  220. 'downloadstr' => formatFreeSpace($download / (1048576)),
  221. 'uploadstr' => formatFreeSpace($upload / (1048576)),
  222. 'totalstr' => formatFreeSpace(($download + $upload) / (1048576))
  223. )
  224. );
  225. }
  226. $download = $row[0];
  227. $upload = $row[1];
  228. $start = $newstart;
  229. } else {
  230. $download += $row[0];
  231. $upload += $row[1];
  232. }
  233. }
  234. if ($upload + $download != 0) {
  235. array_push($month_list, array(
  236. 'user_id' => $username,
  237. 'month' => date('Y-m',strtotime($start)),
  238. 'start' => $start,
  239. 'downloadstr' => formatFreeSpace($download / (1048576)),
  240. 'uploadstr' => formatFreeSpace($upload / (1048576)),
  241. 'totalstr' => formatFreeSpace(($download + $upload) / (1048576))
  242. )
  243. );
  244. }
  245. $tmpl->setloop('month_list', $month_list);
  246. // weekly stats
  247. $xferStats = ($mstart)
  248. ? Xfer::getUsageByDate($username, $mstart, $mend)
  249. : Xfer::getUsageByDate($username);
  250. $start = '';
  251. $download = 0;
  252. $upload = 0;
  253. $week_list = array();
  254. foreach ($xferStats as $row) {
  255. $rtime = strtotime($row[2]);
  256. $newstart = date('d M Y', strtotime('+1 Day last '.$cfg['week_start'], $rtime));
  257. if ($start != $newstart) {
  258. if ($upload + $download != 0) {
  259. array_push($week_list, array(
  260. 'user_id' => $username,
  261. 'month' => $_month,
  262. 'week' => date('Y-m-d',strtotime($start)),
  263. 'start' => $start,
  264. 'downloadstr' => formatFreeSpace($download / (1048576)),
  265. 'uploadstr' => formatFreeSpace($upload / (1048576)),
  266. 'totalstr' => formatFreeSpace(($download+$upload) / (1048576))
  267. )
  268. );
  269. }
  270. $download = $row[0];
  271. $upload = $row[1];
  272. $start = $newstart;
  273. } else {
  274. $download += $row[0];
  275. $upload += $row[1];
  276. }
  277. }
  278. if ($upload + $download != 0) {
  279. array_push($week_list, array(
  280. 'user_id' => $username,
  281. 'month' => $_month,
  282. 'week' => date('Y-m-d',strtotime($start)),
  283. 'start' => $start,
  284. 'downloadstr' => formatFreeSpace($download / (1048576)),
  285. 'uploadstr' => formatFreeSpace($upload / (1048576)),
  286. 'totalstr' => formatFreeSpace(($download+$upload) / (1048576))
  287. )
  288. );
  289. }
  290. $tmpl->setloop('week_list', $week_list);
  291. // daily stats
  292. $xferStats = ($wstart)
  293. ? Xfer::getUsageByDate($username, $wstart, $wend)
  294. : Xfer::getUsageByDate($username);
  295. $start = '';
  296. $download = 0;
  297. $upload = 0;
  298. $day_list = array();
  299. foreach ($xferStats as $row) {
  300. $rtime = strtotime($row[2]);
  301. $newstart = $row[2];
  302. if ($row[2] == date('Y-m-d')) {
  303. if ($user_id == '%') {
  304. $row[0] = $xfer_total['day']['download'];
  305. $row[1] = $xfer_total['day']['upload'];
  306. } else {
  307. $row[0] = $xfer[$username]['day']['download'];
  308. $row[1] = $xfer[$username]['day']['upload'];
  309. }
  310. }
  311. if ($upload + $download != 0) {
  312. array_push($day_list, array(
  313. 'start' => $start,
  314. 'downloadstr' => formatFreeSpace($download / (1048576)),
  315. 'uploadstr' => formatFreeSpace($upload / (1048576)),
  316. 'totalstr' => formatFreeSpace(($download+$upload) / (1048576))
  317. )
  318. );
  319. }
  320. $download = $row[0];
  321. $upload = $row[1];
  322. $start = $newstart;
  323. }
  324. if ($upload + $download != 0) {
  325. array_push($day_list, array(
  326. 'start' => $start,
  327. 'downloadstr' => formatFreeSpace($download / (1048576)),
  328. 'uploadstr' => formatFreeSpace($upload / (1048576)),
  329. 'totalstr' => formatFreeSpace(($download+$upload) / (1048576))
  330. )
  331. );
  332. }
  333. $tmpl->setloop('day_list', $day_list);
  334. //
  335. $tmpl->setvar('_TOTAL', $cfg["_TOTAL"]);
  336. $tmpl->setvar('_SERVERXFERSTATS', $cfg['_SERVERXFERSTATS']);
  337. $tmpl->setvar('_USERDETAILS', $cfg['_USERDETAILS']);
  338. $tmpl->setvar('_USER', $cfg["_USER"]);
  339. $tmpl->setvar('_TOTALXFER', $cfg["_TOTALXFER"]);
  340. $tmpl->setvar('_MONTHXFER', $cfg["_MONTHXFER"]);
  341. $tmpl->setvar('_WEEKXFER', $cfg["_WEEKXFER"]);
  342. $tmpl->setvar('_DAYXFER', $cfg["_DAYXFER"]);
  343. $tmpl->setvar('_DOWNLOAD', $cfg['_DOWNLOAD']);
  344. $tmpl->setvar('_UPLOAD', $cfg['_UPLOAD']);
  345. }
  346. //
  347. $tmpl->setvar('table_admin_border', $cfg["table_admin_border"]);
  348. break;
  349. // default
  350. default:
  351. @error("Invalid Type", "index.php?iid=serverStats", "", array($type));
  352. break;
  353. }
  354. // set vars
  355. $tmpl->setvar('type', $type);
  356. // more vars
  357. tmplSetTitleBar($cfg["pagetitle"].' - Server Stats');
  358. tmplSetFoot();
  359. $tmpl->setvar('enable_multiupload', $cfg["enable_multiupload"]);
  360. $tmpl->setvar('_MULTIPLE_UPLOAD', $cfg['_MULTIPLE_UPLOAD']);
  361. $tmpl->setvar('_ID_IMAGES', $cfg['_ID_IMAGES']);
  362. tmplSetIidVars();
  363. // parse template
  364. $tmpl->pparse();
  365. ?>