Advertisement
Guest User

Untitled

a guest
Apr 5th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. <?php
  2. // error_reporting(E_ALL);
  3.  
  4. $db_host = "xxxx";
  5. $db_user = "root";
  6. $db_pass = "xxxx";
  7. $db_name = "lvps";
  8.  
  9. mysql_connect("$db_host", "$db_user", "$db_pass")or die("cannot connect");
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12. $mmp=mysql_query("SELECT * FROM vps_vps_status");
  13. while ($rows = mysql_fetch_array($mmp)) {
  14. echo $rows['vps_id'];
  15. $dd=getvpsinfo($rows['vps_id'], "ram");
  16. $dd=getvpsinfo($rows['vps_id'], "hdd");
  17. $dd=getvpsinfo($rows['vps_id'], "cpu");
  18. }
  19.  
  20. // neveikia, neaupdatina mysqle ko ?
  21.  
  22. function getvpsinfo ($ifd, $kom) {
  23.  
  24. global $serverIP;
  25. global $serverUSER;
  26. global $serverPASS;
  27. if ($kom == 'ram') {
  28.  
  29.  
  30. $connection=@ssh2_connect($serverIP, 22);
  31.  
  32. ssh2_auth_password($connection,$serverUSER,$serverPASS);
  33. $stdout_stream = ssh2_exec($connection, "vzctl exec2 ".$ifd." free -m | awk {'print $3'}");
  34.  
  35. sleep(1);
  36. $df = 0;
  37. $stderr_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);
  38. while($line = fgets($stderr_stream)) { flush(); echo $line."<br>"; }
  39. while($line = fgets($stdout_stream)) { flush();
  40. //echo $line;
  41. if ($df == 1) {
  42. mysql_query("UPDATE vps_vps_status SET ram='$line' where vps_id='$ifd'");
  43. }
  44. $df=$df+1;
  45.  
  46. }
  47.  
  48. }
  49.  
  50. if ($kom == 'hdd') {
  51. global $serverIP;
  52. $connection=@ssh2_connect($serverIP, 22);
  53. ssh2_auth_password($connection,$serverUSER,$serverPASS);
  54. $stdout_stream = ssh2_exec($connection, "vzctl exec2 ".$ifd." df -h |awk {'print $3'}");
  55.  
  56. sleep(1);
  57. $df = 0;
  58. $stderr_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);
  59. while($line = fgets($stderr_stream)) { flush(); echo $line."<br>"; }
  60. while($line = fgets($stdout_stream)) { flush();
  61. //echo $line;
  62. if ($df==1) {
  63. $mystring = $line;
  64. $findme = 'M';
  65. $pos = strpos($mystring, $findme);
  66.  
  67. if ($pos !== false) {
  68. $hdd=(str_replace("M", "", $line) / 1024);
  69. } else {
  70. $hdd=(str_replace("G", "", $line));
  71. }
  72. mysql_query("UPDATE vps_vps_status SET disk='$hdd' where vps_id='$ifd'");
  73. $array = explode(" ", $line);
  74. $k1 = $array[0];
  75. $k2 = $array[1];
  76. $k3 = $array[2];
  77. $k4 = $array[3];
  78. $k5 = $array[4];
  79. for ($i=13; $i<15; $i++) {
  80. $k4=$k4."".$array[$i];
  81. }
  82. echo round($k4,1);
  83. }
  84. $df = $df + 1;
  85. }
  86.  
  87. }
  88. if ($kom == 'cpu') {
  89. global $serverIP;
  90. $connection=@ssh2_connect($serverIP, 22);
  91. ssh2_auth_password($connection,$serverUSER,$serverPASS);
  92. $stdout_stream = ssh2_exec($connection, "vzctl exec2 ".$ifd." vmstat 1 5 |awk '{print $15}' > file");
  93. sleep(1);
  94. $df = 0;
  95. $stderr_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);
  96. sleep(5);
  97. $stdout_stream = ssh2_exec($connection, 'cat file');
  98. sleep(1);
  99. $df = 0;
  100. $stderr_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);
  101. while($line = fgets($stderr_stream)) { flush(); echo $line."<br>"; }
  102. while($line = fgets($stdout_stream)) { flush();
  103. //echo $line;
  104.  
  105. if ($df==4) {
  106. $line=100-$line;
  107. echo $line;
  108. mysql_query("UPDATE vps_vps_status SET cpu='$line' where vps_id='$ifd'");
  109. }
  110. $df = $df + 1;
  111. }
  112.  
  113. }
  114. if ($kom == 'uptime') {
  115. global $serverIP;
  116. $connection=@ssh2_connect($serverIP, 22);
  117. ssh2_auth_password($connection,$serverUSER,$serverPASS);
  118. $stdout_stream = ssh2_exec($connection, "vzctl exec2 ".$ifd." uptime");
  119.  
  120. sleep(1);
  121. $df = 0;
  122. $stderr_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);
  123. while($line = fgets($stderr_stream)) { flush(); echo $line."<br>"; }
  124. while($line = fgets($stdout_stream)) { flush();
  125. echo $line;
  126. if ($df == 1) {
  127. mysql_query("UPDATE vps_vps_status SET uptime='$line' where vps_id='$ifd'");
  128. }
  129. $df=$df+1;
  130.  
  131. }
  132.  
  133. }
  134. }
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement