Heim > php教程 > php手册 > PHP监控linux服务器负载

PHP监控linux服务器负载

WBOY
Freigeben: 2016-06-13 10:38:19
Original
954 Leute haben es durchsucht

  由于Web Server以及PHP的实现方式所限,我们在现实环境中很难利用PHP去调用一些Linux中需要root权限才能执行的程序,对此,我从网上找到另外一种方式来绕开这个限制。首先先写个c程序中转调用系统命令,然后用PHP去执行此c程序。

  c程序

  首先写个c文件,比如/usr/local/ismole/w.c

 

<ol class="dp-c">
<li class="alt"><span><span class="comment">//note 由于wordpress编辑器的原因,请将此代码中头文件多余的空格去除 </span> </span></li>
<li><span><span class="preprocessor">#include  </span> </span></li>
<li class="alt"><span><span class="preprocessor">#include  </span> </span></li>
<li><span><span class="preprocessor">#include  </span> </span></li>
<li class="alt"><span><span class="preprocessor">#include  </span><span> </span></span></li>
<li><span> </span></li>
<li class="alt">
<span class="keyword">int</span><span> main()  </span>
</li>
<li><span>{  </span></li>
<li class="alt"><span>    uid_t uid ,euid;  </span></li>
<li><span> </span></li>
<li class="alt">
<span>    </span><span class="comment">//note 获得当前的uid </span><span> </span>
</li>
<li><span>    uid = getuid();  </span></li>
<li class="alt">
<span>    </span><span class="comment">//note 获得当前euid </span><span> </span>
</li>
<li><span>    euid = geteuid();  </span></li>
<li class="alt"><span> </span></li>
<li>
<span>    </span><span class="comment">//note 交换这两个id </span><span> </span>
</li>
<li class="alt">
<span>    </span><span class="keyword">if</span><span>(setreuid(euid, uid))  </span>
</li>
<li>
<span>        perror(</span><span class="string">"setreuid"</span><span>);  </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span>    </span><span class="comment">//note 执行将要执行linux系统命令 </span><span> </span>
</li>
<li class="alt">
<span>    system(</span><span class="string">"/usr/bin/w"</span><span>);  </span>
</li>
<li>
<span>    </span><span class="keyword">return</span><span> 0;  </span>
</li>
<li class="alt"><span>}  </span></li>
</ol>
Nach dem Login kopieren

  //note 由于wordpress编辑器的原因,请将此代码中头文件多余的空格去除

  编译该文件gcc -o ipt -Wall w.c,这时会在当前目录下生成程序w。

  改变此程序的属主chmod u+s ./w。

  PHP执行

  文件内容如下,放在web目录下,访问就会输出当前的服务器负载情况。

 

<ol class="dp-c">
<li class="alt"><span><span></span></span></li>
<li>
<span class="comment">//note key的验证过程 </span><span> </span>
</li>
<li class="alt">
<span class="keyword">if</span><span>(</span><span class="vars">$key</span><span> != </span><span class="vars">$authkey</span><span>) {  </span>
</li>
<li>
<span class="comment">//        exit(key error); </span><span> </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="vars">$last_line</span><span> = </span><span class="func">exec</span><span>(</span><span class="string">/usr/local/ismole/w</span><span>, </span><span class="vars">$retval</span><span>);  </span>
</li>
<li><span> </span></li>
<li class="alt">
<span class="vars">$returnArray</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"load average: "</span><span>, </span><span class="vars">$retval</span><span>[0]);  </span>
</li>
<li>
<span class="vars">$returnString</span><span> = </span><span class="vars">$returnArray</span><span>[1];  </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$returnString</span><span>;  </span>
</li>
</ol>
Nach dem Login kopieren

  按照上面的实例,我们可以用PHP来做任何我们想执行的Linux系统命令,SVN更新,服务器监控,备份,恢复,日常维护等等。比如《利用SVN搭建测试服务器》

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage