> For the complete documentation index, see [llms.txt](https://kb.vimzaa.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kb.vimzaa.com/cpanel-hosting/cpanel-advanced/add-monitoring-script-to-server-for-monitoring-top-iostat-etc.md).

# Add monitoring script to server for monitoring top, iostat etc

Create a directory under /var/log/server-status

Inside this directory, create a file called ‘getstats.sh’ and paste the following contents…

```
#!/bin/sh
path="/var/log/server-status"
ddd=`date +%Y-%m-%d`
logfile=$path/$ddd/logfile.txt

### Check if direction to store log exists, if doesn't - create it ###
if [ ! -d $path/$ddd ]; then
    mkdir $path/$ddd
fi

### Add blank line and head 5 of top on every script run ###
echo >> $logfile
echo "!-------------------------------------------- top 20" >> $logfile
COLUMNS=512 /usr/bin/top -cSb -n 1 | head -20                           >> $logfile

echo "!---------------------------------------- vmstat 1 4" >> $logfile
/usr/bin/vmstat 1 4                                         >> $logfile

### Check if load average is greater or equal 7 if it does - collect needed stats ###

if [ `cat /proc/loadavg | /usr/bin/awk '{ print $1 }' | /usr/bin/cut -d. -f1-1` -ge 3 ]
then

### INSERT custom gathering commands after this line, they are executed only when LA is above 7

###

echo "!---------------------------------- netstat by state" >> $logfile
/bin/netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c        >> $logfile

echo "!-------------------------------- ps by memory usage" >> $logfile
ps aux | sort -nk +4 | tail                                 >> $logfile

echo "!------------------------------------- iotop -b -n 3" >> $logfile
/usr/sbin/iotop -b -o -n 3                                  >> $logfile

echo "!------------------------------------------- ps axuf" >> $logfile
ps axuf                                                     >> $logfile

echo "!---------------------------- mysqladmin processlist" >> $logfile
/usr/bin/mysqladmin processlist -v                          >> $logfile

fi

### Do something more if load average does not match threshold ###

### Remove directories older then 30 days ###
cd $path
ls -1r|grep -v getstatus|sed -n '30,$p'|xargs -i rm -rf "{}"
```

You will also need to create a cron for this…

```
vi /etc/cron.d/getstats
```

```
* * * * * root /usr/bin/flock -n /var/run/cloudlinux_getstats.cronlock /bin/sh /var/log/server-status/getstats.sh >/dev/null 2>&1
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://kb.vimzaa.com/cpanel-hosting/cpanel-advanced/add-monitoring-script-to-server-for-monitoring-top-iostat-etc.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
