1
0

functions.core.netstat.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /* $Id: functions.core.netstat.php 3326 2008-01-23 14:48:25Z lordnor $ */
  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. /**
  16. * netstatConnectionsSum
  17. *
  18. * @return int
  19. */
  20. function netstatConnectionsSum() {
  21. global $cfg;
  22. switch ($cfg["_OS"]) {
  23. case 1: // linux
  24. return intval(trim(shell_exec($cfg['bin_netstat']." -e -p --tcp -n 2> /dev/null | ".$cfg['bin_grep']." -v root | ".$cfg['bin_grep']." -v 127.0.0.1 | ".$cfg['bin_grep']." -cE '.*(python|transmissionc|wget|nzbperl|java).*'")));
  25. case 2: // bsd
  26. $processUser = posix_getpwuid(posix_geteuid());
  27. $webserverUser = $processUser['name'];
  28. return intval(trim(shell_exec($cfg['bin_sockstat']." -c -4 -P tcp | ".$cfg['bin_grep']." -v 127.0.0.1 | ".$cfg['bin_grep']." -cE '".$webserverUser.".+(python|transmissi|wget|nzbperl|java)'")));
  29. }
  30. return 0;
  31. }
  32. /**
  33. * netstatConnections
  34. *
  35. * @param $transfer
  36. * @return int
  37. */
  38. function netstatConnections($transfer) {
  39. return netstatConnectionsByPid(getTransferPid($transfer));
  40. }
  41. /**
  42. * netstatConnectionsByPid
  43. *
  44. * @param $transferPid
  45. * @return int
  46. */
  47. function netstatConnectionsByPid($transferPid) {
  48. global $cfg;
  49. switch ($cfg["_OS"]) {
  50. case 1: // linux
  51. return trim(shell_exec($cfg['bin_netstat']." -e -p --tcp --numeric-hosts --numeric-ports 2> /dev/null | ".$cfg['bin_grep']." -v root | ".$cfg['bin_grep']." -v 127.0.0.1 | ".$cfg['bin_grep']." -c \"".$transferPid ."/\""));
  52. case 2: // bsd
  53. $processUser = posix_getpwuid(posix_geteuid());
  54. $webserverUser = $processUser['name'];
  55. return intval(trim(shell_exec($cfg['bin_sockstat']." -P tcp -c | ".$cfg['bin_grep']." -cE ".$webserverUser.".+".$transferPid)));
  56. }
  57. }
  58. /**
  59. * netstatPortList
  60. *
  61. * @return string
  62. */
  63. function netstatPortList() {
  64. global $cfg;
  65. $retStr = "";
  66. switch ($cfg["_OS"]) {
  67. case 1: // linux
  68. // not time-critical (only used on allServices-page), use the
  69. // generic and correct way :
  70. // array with all clients
  71. $clients = array('tornado', 'transmission', 'wget', 'nzbperl', 'azureus');
  72. // get informations
  73. foreach ($clients as $client) {
  74. $ch = ClientHandler::getInstance($client);
  75. $retStr .= shell_exec($cfg['bin_netstat']." -e -l -p --tcp --numeric-hosts --numeric-ports 2> /dev/null | ".$cfg['bin_grep']." -v root | ".$cfg['bin_grep']." ". $ch->binSocket ." | ".$cfg['bin_awk']." '{print \$4}' | ".$cfg['bin_awk']." 'BEGIN{FS=\":\"}{print \$2}'");
  76. }
  77. break;
  78. case 2: // bsd
  79. $processUser = posix_getpwuid(posix_geteuid());
  80. $webserverUser = $processUser['name'];
  81. $retStr .= shell_exec($cfg['bin_sockstat']." -4 -l -P tcp | ".$cfg['bin_awk']." '/(python|transmissi|wget|nzbperl|java).+\*:[0-9]/ {split(\$6, a, \":\");print a[2]}'");
  82. break;
  83. }
  84. return $retStr;
  85. }
  86. /**
  87. * netstatPort
  88. *
  89. * @param $transfer
  90. * @return int
  91. */
  92. function netstatPort($transfer) {
  93. return netstatPortByPid(getTransferPid($transfer));
  94. }
  95. /**
  96. * netstatPortByPid
  97. *
  98. * @param $transferPid
  99. * @return int
  100. */
  101. function netstatPortByPid($transferPid) {
  102. global $cfg;
  103. switch ($cfg["_OS"]) {
  104. case 1: // linux
  105. return trim(shell_exec($cfg['bin_netstat']." -l -e -p --tcp --numeric-hosts --numeric-ports 2> /dev/null | ".$cfg['bin_grep']." -v root | ".$cfg['bin_grep']." \"".$transferPid ."/\" | ".$cfg['bin_awk']." '{print \$4}' | ".$cfg['bin_awk']." 'BEGIN{FS=\":\"}{print \$2}'"));
  106. case 2: // bsd
  107. $processUser = posix_getpwuid(posix_geteuid());
  108. $webserverUser = $processUser['name'];
  109. //delete return shell_exec($cfg['bin_sockstat']." | ".$cfg['bin_awk']." '/".$webserverUser.".*".$transferPid.".*tcp4 .*\*:[0-9]/ {split(\$6, a, \":\");print a[2];nextfile}'");
  110. return shell_exec($cfg['bin_sockstat']." -4 -P tcp -l | ".$cfg['bin_awk']." '/".$webserverUser.".+".$transferPid.".+:[0-9]/ {split(\$6, a, \":\"); if (a[2] != 80) {print a[2]; nextfile}}'");
  111. }
  112. }
  113. /**
  114. * netstatHostList
  115. *
  116. * @return string
  117. */
  118. function netstatHostList() {
  119. global $cfg;
  120. $retStr = "";
  121. switch ($cfg["_OS"]) {
  122. case 1: // linux
  123. // not time-critical (only used on allServices-page), use the
  124. // generic and correct way :
  125. // array with all clients
  126. $clients = array('tornado', 'transmission', 'wget', 'nzbperl', 'azureus');
  127. // get informations
  128. foreach($clients as $client) {
  129. $ch = ClientHandler::getInstance($client);
  130. $retStr .= shell_exec($cfg['bin_netstat']." -e -p --tcp --numeric-hosts --numeric-ports 2> /dev/null | ".$cfg['bin_grep']." -v root | ".$cfg['bin_grep']." -v 127.0.0.1 | ".$cfg['bin_grep']." ". $ch->binSocket ." | ".$cfg['bin_awk']." '{print \$5}'");
  131. }
  132. break;
  133. case 2: // bsd
  134. $processUser = posix_getpwuid(posix_geteuid());
  135. $webserverUser = $processUser['name'];
  136. $retStr .= shell_exec($cfg['bin_sockstat']." -4 -P tcp -c | ".$cfg['bin_awk']." '/".$webserverUser.".+(python|transmissi|nzbperl|wget|java)/ {split(\$7, a, \":\"); print a[1]}'");
  137. break;
  138. }
  139. return $retStr;
  140. }
  141. /**
  142. * netstatHosts
  143. *
  144. * @param $transfer
  145. * @return array
  146. */
  147. function netstatHosts($transfer) {
  148. return netstatHostsByPid(getTransferPid($transfer));
  149. }
  150. /**
  151. * netstatHostsByPid
  152. *
  153. * @param $transferPid
  154. * @return array
  155. */
  156. function netstatHostsByPid($transferPid) {
  157. global $cfg;
  158. switch ($cfg["_OS"]) {
  159. case 1: // linux
  160. $hostList = shell_exec($cfg['bin_netstat']." -e -p --tcp --numeric-hosts --numeric-ports 2> /dev/null | ".$cfg['bin_grep']." -v root | ".$cfg['bin_grep']." -v 127.0.0.1 | ".$cfg['bin_grep']." \"".$transferPid."/\" | ".$cfg['bin_awk']." '{print \$5}'");
  161. break;
  162. case 2: // bsd
  163. $processUser = posix_getpwuid(posix_geteuid());
  164. $webserverUser = $processUser['name'];
  165. $hostList = shell_exec($cfg['bin_sockstat']." -P tcp -4 -c | ".$cfg['bin_awk']." '/".$webserverUser.".+".$transferPid."/ {print \$7}'");
  166. break;
  167. }
  168. $retVal = array();
  169. $hostAry = explode("\n", $hostList);
  170. foreach ($hostAry as $hostLine) {
  171. $hostLineAry = explode(':', trim($hostLine));
  172. $retVal[$hostLineAry[0]] = isset($hostLineAry[1]) ? $hostLineAry[1] : "";
  173. }
  174. return $retVal;
  175. }
  176. ?>