• 0

PHP CPU Load per page script?


Question

I was looking through my emails today and saw a email from my web host about CPU Usage, I was alarmed!

To cut a long story short, I found out that my site was using 10x the CPU Usage recommended, I have been using my host for years without any problems so want to find the source of my problem.

I was wondering if there was a script or program or something, (Thats gramatically bad in so many ways!!) which would tell me what load/usage of my CPU each script/page is using, so I can track down the source of the problem.

I have looked and cannot find anything which does this.

Any help would be appreciated!

Tim

Link to comment
https://www.neowin.net/forum/topic/872122-php-cpu-load-per-page-script/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

You should ask your host to identify the problem for you. The told you your site uses too much CPU, so they need to tell you WHAT processes they saw running that was hogging the CPU. I'd also ask them if this is the first time it's happened or if the CPU usage has been constantly climbing.

Most scripts aren't going to help you out since majority of them are just bash scripts and/or commands to be used with root SSH, and I'm guessing you just have a shared hosting account.

#!/bin/bash
# Shell script to get uptime, disk usage, cpu usage, RAM usage,system load,etc.
# from multiple Linux servers and output the information on a single server
# in html format. Read below for usage/installation info
# *---------------------------------------------------------------------------*
# * dig_remote_linux_server_information.bash,v0.1, last updated on 25-Jul-2005*
# * Copyright (c) 2005 nixCraft project                                       *
# * Comment/bugs: http://cyberciti.biz/fb/                                    *
# * Ref url: http://cyberciti.biz/nixcraft/forum/viewtopic.php?t=97           *
# * This script is licensed under GNU GPL version 2.0 or above                *
# *---------------------------------------------------------------------------*
# *  Installation Info                                                        *
# ----------------------------------------------------------------------------*
# You need to setup ssh-keys to avoid password prompt, see url how-to setup
# ssh-keys:
# cyberciti.biz/nixcraft/vivek/blogger/2004/05/ssh-public-key-based-authentication.html
#
# [1] You need to setup correct VARIABLES script:
#
# (a) Change Q_HOST to query your host to get information
# Q_HOST="192.168.1.2 127.0.0.1 192.168.1.2"
#
# (b) Setup USR, who is used to connect via ssh and already setup to connect
# via ssh-keys
# USR="nixcraft"
#
# (c)Show warning if server load average is below the limit for last 5 minute.
# setup LOAD_WARN as per your need, default is 5.0
#
# LOAD_WARN=5.0
#
# (d) Setup your network title using MYNETINFO
# MYNETINFO="My Network Info"
#
# (e) Save the file
#
# Please refer to forum topic on this script:
# Also download the .gif files and put them in your output dir
#
# ----------------------------------------------------------------------------
# Execute script as follows (and copy .gif file in this dir) :
# this.script.name > /var/www/html/info.html
# ============================================================================
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------

# SSH SERVER HOST IPS, setup me
# Change this to query your host
Q_HOST="192.168.1.2 127.0.0.1 192.168.1.2"

# SSH USER, change me
USR="nixcraft"

# Show warning if server load average is below the limit for last 5 minute
LOAD_WARN=5.0

# Your network info
MYNETINFO="My Network Info"
#
# if it  is run as cgi we can do reload stuff too :D
PBY='Powered by &lt;a href="http://cyberciti.biz/download/">script</a> font colours
GREEN='&lt;font color="#00ff00"&gt;'
RED='&lt;font color="#ff0000"&gt;'
NOC='&lt;/font&gt;'
LSTART='
&lt;ul&gt;
&lt;li&gt;'
LEND='&lt;/li&gt;
&lt;/ul&gt;

'
# Local path to ssh and other bins
SSH="/usr/bin/ssh"
PING="/bin/ping"
NOW="$(date)"

## functions ##
writeHead(){
 echo '&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Network Status&lt;/TITLE&gt;&lt;/HEAD&gt;
 &lt;BODY alink="#0066ff" bgcolor="#000000" link="#0000ff" text="#ccddee" vlink="#0033ff"&gt;'
 echo '&lt;CENTER&gt;&lt;H1&gt;'
 echo "$MYNETINFO&lt;/H1&gt;"
 echo "Generated on $NOW"
 echo '&lt;/CENTER&gt;'

}

writeFoot(){
 echo "&lt;HR&gt;&lt;center&gt;$PBY&lt;/center&gt;"
  echo "&lt;/BODY&gt;&lt;/HTML&gt;"
}

## main ##

writeHead
echo '&lt;TABLE WIDTH=100% BORDER=2 BORDERCOLOR="#000080" CELLPADDING=4 CELLSPACING=4 FRAME=HSIDES RULES=NONE" &gt;'
echo '&lt;TR VALIGN=TOP&gt;'
for host in $Q_HOST
do
  #echo '&lt;TD WIDTH=33% BGCOLOR="#0099ff"&gt;'
  echo '&lt;TD BGCOLOR="#0099ff"&gt;'
  _CMD="$SSH $USR@$host"
  rhostname="$($_CMD hostname)"

  ruptime="$($_CMD uptime)"
  if $(echo $ruptime | grep -E "min|days" &gt;/dev/null); then
    x=$(echo $ruptime | awk '{ print $3 $4}')
  else
    x=$(echo $ruptime | sed s/,//g| awk '{ print $3 " (hh:mm)"}')
  fi
  ruptime="$x"

  rload="$($_CMD uptime |awk -F'average:' '{ print $2}')"
  x="$(echo $rload | sed s/,//g | awk '{ print $2}')"
  y="$(echo "$x &gt;= $LOAD_WARN" | bc)"
  [ "$y" == "1" ] &amp;&amp; rload="$RED $rload (High) $NOC" || rload="$GREEN $rload (Ok) $NOC"

  rclock="$($_CMD date +"%r")"
  rtotalprocess="$($_CMD ps axue | grep -vE "^USER|grep|ps" | wc -l)"
  rfs="$($_CMD df -hT | grep -vE "^Filesystem|shm" \
  | awk 'BEGIN{print "
&lt;ul&gt;"}{w=sprintf("%d",$6);print "
&lt;li&gt;" $7 \
  "            " $6 \
  "(" $2 ")&lt;BR&gt; &lt;img src=\"indicator.gif\" height=\"4\" width=\"" w "\"&gt; \
  &lt;BR&gt;&lt;img src=\"graph.gif\"> &lt;BR&gt;" $4"/"$3 "&lt;/li&gt;

"}END{ print "&lt;/ul&gt;

"}')"

  rusedram="$($_CMD free -mto | grep Mem: | awk '{ print $3 " MB" }')"
  rfreeram="$($_CMD free -mto | grep Mem: | awk '{ print $4 " MB" }')"
  rtotalram="$($_CMD free -mto | grep Mem: | awk '{ print $2 " MB" }')"

  $PING -c1  $host&gt;/dev/null
  if [ "$?" != "0" ] ; then
    rping="$RED Failed $NOC"
  else
    rping="$GREEN Ok $NOC"
    echo "&lt;b&gt;&lt;u&gt;$rhostname&lt;/u&gt;&lt;/b&gt;&lt;BR&gt;"
    echo "Ping status: $rping&lt;BR&gt;"
    echo "Time: $rclock&lt;BR&gt;"
    echo "Uptime: $ruptime &lt;BR&gt;"
    echo "Load avarage: $LSTART $rload $LEND"
    echo "Total running process: $LSTART $rtotalprocess $LEND"
    echo "Disk status:"
    echo "$rfs"
    echo "Ram/swap status:
&lt;ul&gt;"
    echo "
&lt;li&gt;Used RAM: $rusedram&lt;/li&gt;

"
    echo "
&lt;li&gt;Free RAM: $rfreeram&lt;/li&gt;

"
    echo "
&lt;li&gt;Total RAM: $rtotalram &lt;/li&gt;
&lt;/ul&gt;

"
  fi   

  echo "&lt;/td&gt;

"
done
  echo "&lt;/tr&gt;
&lt;/table&gt;

"
writeFoot

That would work out if this was your own server you had access to.

Just tell your host to give you an output of the CPU usage when your account hit its peak. It's not that hard so they should be able to do this for you immediately. :)

  • 0

FYI: I have a reseller account.

The account the high CPU usage is on is a fairly new account. They told me the script which has high CPU usage, i'm not entirely sure why its going so high - Apart from the 5+ xml parses in their, which may not be helping?

  • 0

Ah, well a reseller account functions the same as a shared account; You just have WHM access. You're still sharing the server with other users. Basically you don't have access to root SSH and/or your own server configuration. That's what I mean. :)

Can you ask them if a single IP keeps hitting the new account and if so, which URL it keeps hitting? They should also be able to determine more info than just telling you the script.

Which by the way, what script are you using?

  • 0
  On 04/02/2010 at 17:28, Tjcrazy said:

The script is my own custom made one, built using php. It basically pulls loads of xml information from numerous xml files.

You should probably cache the page being created from the XML, if possible, using either a file on the filesystem or a memory caching extension such as APC (it's unlikely that your host has this enabled).

If you give us more information about this script, we can provide better advice.

  • 0

I did look at caching, but the xml files update themselves, and thats where the main content for the website comes from. (http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=bbcradio1&api_key=c910b0bc1e2d4e64964ebcd2d69c255c&limit=500)

THe current.php script is the script which is parsing the XML information basically, theres around 5 simplexml and xml parsing scripts.

Would parsing by JS be any easier?

  • 0
  On 04/02/2010 at 20:54, Tjcrazy said:

I did look at caching, but the xml files update themselves, and thats where the main content for the website comes from. (http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=bbcradio1&api_key=c910b0bc1e2d4e64964ebcd2d69c255c&limit=500)

THe current.php script is the script which is parsing the XML information basically, theres around 5 simplexml and xml parsing scripts.

Would parsing by JS be any easier?

That's no reason not to cache the generated pages, you could do it for something as low as 5 seconds.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • That’s why I love classic Notepad from W10 IOT LTSC. It opens up instantly, uses very little resources, is just a plain notepad and nothing more. the title should be notepad has lost focus.
    • Seeing my profile picture in Notepad creeped me out. I do not appreciate that Copilot is opt-out rather than opt-in. I don't want Notepad to have connectivity. The other improvements are fine, including the upcoming Markdown support (although I hate Markdown's handling of newlines with a passion).
    • More like Trelane: https://memory-alpha.fandom.com/wiki/Trelane But, yes, Q.  
    • This would come in handy to me as an Uber driver. My biggest pet peeve is when passengers listen to their own audio on their phones without using headphones when they can hear I'm already playing music through my car's head unit.
    • Astonishing new tech could kill headphones forever as it bends sound right into ears by Sayan Sen Imagine being able to listen to your favorite song or podcast out loud without disturbing anyone nearby even without wearing headphones. That’s what a team at Penn State University has been working on. Led by acoustics professor Yun Jing, they’ve come up with a clever way to create invisible audio zones called “audible enclaves” where sound can be heard only at one exact spot. They use ultrasound, which is normally inaudible to people, along with something called acoustic metasurfaces—tiny lenses that can bend sound in specific directions. By combining two ultrasound beams that travel in curved paths and meet at a single point, they’re able to make sound audible only at that intersection. As Jing explained, “The person standing at that point can hear sound, while anyone standing nearby would not. This creates a privacy barrier between people for private listening.” To make this happen, the system includes two ultrasonic speakers and the metasurface lenses, which were 3D printed by Xiaoxing Xia from Lawrence Livermore National Lab. Each beam has a slightly different frequency, and when they meet, a local reaction makes the sound audible. Neither beam is loud on its own—the sound only forms at that shared point. Jia-Xin “Jay” Zhong, one of the researchers, shared how they tested the idea: “We used a simulated head and torso dummy with microphones inside its ears to mimic what a human being hears at points along the ultrasonic beam trajectory, as well as a third microphone to scan the area of intersection. We confirmed that sound was not audible except at the point of intersection, which creates what we call an enclave.” One of the biggest advantages of their approach is that it works across a wide range of sound frequencies—between 125 Hz and 4 kHz, which covers most of what people can hears. Even in rooms where sound usually bounces around, their system held up well. And it’s surprisingly compact too: the whole setup measures about 16 centimeters, roughly the size of a pencil case. “We essentially created a virtual headset,” Zhong said. In practice, it means that someone standing in the audible enclave can hear what’s being played clearly, while everyone else around hears nothing at all. That could be especially useful in shared spaces like cars, classrooms, or open offices. Right now, the sound can travel about one meter and hits around 60 decibels which is similar to regular talking volume. The team believes they can push those limits further by using stronger ultrasound. All this might seem futuristic, but it’s grounded in solving a basic problem: how to direct sound only where it’s needed. If you’re into tech and sound design, this could open up a whole new world of personalized audio experiences. Source: Penn State, PNAS | Image via Depositphotos This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
  • Recent Achievements

    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
    • One Month Later
      Ian_ earned a badge
      One Month Later
    • Dedicated
      MacDaddyAz earned a badge
      Dedicated
    • Explorer
      cekicen went up a rank
      Explorer
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      ATLien_0
      211
    3. 3
      Michael Scrip
      197
    4. 4
      Xenon
      149
    5. 5
      +FloatingFatMan
      125
  • Tell a friend

    Love Neowin? Tell a friend!