├── README.md └── hetrixtools_agent.php /README.md: -------------------------------------------------------------------------------- 1 | # HetrixTools Server Monitoring Agent (PHP) 2 | 3 | Installation instructions: https://docs.hetrixtools.com/install-the-php-version-of-the-server-monitoring-agent/ 4 | 5 | Server Monitoring Documentation: https://docs.hetrixtools.com/category/server-monitor/ 6 | -------------------------------------------------------------------------------- /hetrixtools_agent.php: -------------------------------------------------------------------------------- 1 | $info[1],'nice' => $info[2],'sys' => $info[3],'idle' => $info[4],'iowait' => $info[5]); 136 | 137 | } 138 | 139 | } 140 | 141 | // Output 142 | return $cores; 143 | 144 | } 145 | 146 | // Function used to compare the two CPU stats samples 147 | function get_cpu_usage($stat1,$stat2) { 148 | 149 | // Number of cores from both samples should match 150 | if(count($stat1) !== count($stat2)) {return false;} 151 | 152 | // Set initial values 153 | $total_idle = 0; $total_iowait = 0; 154 | 155 | // Go through all the cores 156 | for($i=0,$l=count($stat1);$i<$l;$i++) { 157 | 158 | // Initiate the difference array 159 | $dif = array(); 160 | 161 | // Calculate the differences for this specific core 162 | $dif['user'] = $stat2[$i]['user'] - $stat1[$i]['user']; 163 | $dif['nice'] = $stat2[$i]['nice'] - $stat1[$i]['nice']; 164 | $dif['sys'] = $stat2[$i]['sys'] - $stat1[$i]['sys']; 165 | $dif['idle'] = $stat2[$i]['idle'] - $stat1[$i]['idle']; 166 | $dif['iowait'] = $stat2[$i]['iowait'] - $stat1[$i]['iowait']; 167 | 168 | // Sum up the total core usage 169 | $total = array_sum($dif); 170 | 171 | // Calculate this core's idle percentage 172 | $total_idle += (1000*$dif['idle']/$total)/10; 173 | 174 | // Calculate this core's iowait percentage 175 | $total_iowait += (1000*$dif['iowait']/$total)/10; 176 | 177 | } 178 | 179 | // Calculate the CPU Usage for all available cores 180 | $cpu_usage = round(100 - $total_idle / count($stat1),2); 181 | 182 | // Calculate the IOWait for all the available cores 183 | $cpu_iowait = round($total_iowait / count($stat1),2); 184 | 185 | // Output 186 | return array($cpu_usage,$cpu_iowait,count($stat1)); 187 | 188 | } 189 | 190 | // Function used to find the array element that contains a given string 191 | function find_in_array($needle,$subject) { 192 | 193 | // Go through all the array elements 194 | foreach($subject as $key => $value) { 195 | 196 | // If the current element string contains what we need, output the element string 197 | if(stristr($value,$needle)) { 198 | 199 | // Output 200 | return $value; 201 | 202 | } 203 | 204 | } 205 | 206 | } 207 | 208 | // Function used to get the current timestamp in microseconds 209 | function microtime_float() { 210 | 211 | // Get microtime 212 | list($usec,$sec) = explode(" ",microtime()); 213 | 214 | // Output 215 | return ((float)$usec + (float)$sec); 216 | 217 | } 218 | 219 | /////////////// 220 | // Functions // 221 | /////////////// 222 | 223 | // Start time 224 | $start = microtime_float(); 225 | 226 | // Get initial stats 227 | $cpu1 = get_cpu_stats(); 228 | $net1 = get_network_stats($inet); 229 | 230 | // Sleep 231 | $sleep = 60 - intval(date('s')); 232 | sleep($sleep); 233 | 234 | // Get secondary stats 235 | $cpu2 = get_cpu_stats(); 236 | $net2 = get_network_stats($inet); 237 | 238 | // End time 239 | $end = microtime_float(); 240 | 241 | // Calculate running time 242 | $seconds = round($end - $start); 243 | 244 | // Calculate Usage 245 | $cpu = get_cpu_usage($cpu1,$cpu2); 246 | $net = get_network_usage($net1,$net2); 247 | 248 | // Operating System 249 | if(is_readable('/etc/lsb-release')) { 250 | $os = file('/etc/lsb-release'); 251 | $os = explode('"',$os[3]); 252 | $os = $os[1]; 253 | } 254 | elseif(is_readable('/etc/debian_version')) {$os = "Debian ".file_get_contents('/etc/debian_version');} 255 | elseif(is_readable('/etc/redhat-release')) {$os = file_get_contents('/etc/redhat-release');} 256 | elseif(is_readable('/proc/sys/kernel/osrelease')) {$os = "Linux ".file_get_contents('/proc/sys/kernel/osrelease');} 257 | else {$os = "Linux";} 258 | $os = base64_encode($os); 259 | 260 | // Uptime 261 | $uptime = intval(file_get_contents('/proc/uptime')); 262 | 263 | // CPU Info 264 | $cpu_info = file('/proc/cpuinfo'); 265 | 266 | // CPU Model 267 | $cpu_model = explode(": ",$cpu_info[4]); 268 | $cpu_model = base64_encode($cpu_model[1]); 269 | 270 | // CPU Speed 271 | $cpu_speed = explode(": ",$cpu_info[7]); 272 | $cpu_speed = intval($cpu_speed[1]); 273 | 274 | // CPU Cores 275 | $cpu_cores = $cpu[2]; 276 | 277 | // CPU Usage 278 | $cpu_usage = $cpu[0]; 279 | 280 | // CPU IOWAIT 281 | $cpu_iowait = $cpu[1]; 282 | 283 | // RAM Info 284 | $ram_info = file('/proc/meminfo'); 285 | 286 | // RAM Size 287 | $ram_size = intval_from_ram('MemTotal',$ram_info); 288 | 289 | // RAM Usage 290 | $ram_free = intval_from_ram('MemFree',$ram_info) + intval_from_ram('Buffers',$ram_info) + intval_from_ram('Cached',$ram_info); 291 | $ram_usage = round(100 - (($ram_free*100)/$ram_size),2); 292 | 293 | // Swap Size 294 | $swap_size = intval_from_ram('SwapTotal',$ram_info); 295 | 296 | if($swap_size > 0) { // Server swap exists 297 | 298 | // Swap Usage 299 | $swap_free = intval_from_ram('SwapFree',$ram_info); 300 | $swap_usage = round(100 - (($swap_free*100)/$swap_size),2); 301 | 302 | } else { // Server does not have swap 303 | 304 | // Set Swap usage to 0 305 | $swap_usage = 0; 306 | 307 | } 308 | 309 | // Disk Usage 310 | $disk_total = disk_total_space(dirname(__FILE__)); 311 | $disk_used = $disk_total-disk_free_space(dirname(__FILE__)); 312 | $disk_path = explode("/",dirname(__FILE__)); 313 | $disk_path = "/".$disk_path[1]; 314 | $disk = base64_encode($disk_path.",".intval($disk_total).",".intval($disk_used).";"); 315 | 316 | // Network Usage 317 | $rx = round($net[0]/$seconds); // Incoming 318 | $tx = round($net[1]/$seconds); // Outgoing 319 | 320 | // Arrange the post data 321 | $post_data = "$os|$uptime|$cpu_model|$cpu_speed|$cpu_cores|$cpu_usage|$cpu_iowait|$ram_size|$ram_usage|$swap_size|$swap_usage|$disk|$rx|$tx|"; 322 | $post = "v=$version&a=1&s=$SID&d=$post_data"; 323 | 324 | // Log the current post string (for debugging) 325 | file_put_contents(dirname(__FILE__).'/hetrixtools_agent.log',$post); 326 | 327 | // Post the data to HetrixTools 328 | $ch = curl_init('https://sm.hetrixtools.net'); 329 | curl_setopt($ch,CURLOPT_POST,1); 330 | curl_setopt($ch,CURLOPT_POSTFIELDS,$post); 331 | curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); 332 | curl_setopt($ch,CURLOPT_HEADER,0); 333 | curl_setopt($ch,CURLOPT_RETURNTRANSFER,0); 334 | curl_exec($ch); 335 | 336 | ?> 337 | --------------------------------------------------------------------------------