How to Get CPU Usage and Ram Usage Without Exec

How to get CPU usage and RAM usage without exec?

Use PHPSysInfo library

phpSysInfo is a open source PHP script that displays information about the host being accessed. It will displays things like:

  • Uptime
  • CPU
  • Memory
  • SCSI, IDE, PCI
  • Ethernet
  • Floppy
  • Video Information

It directly parsed parses /proc and does not use exec.


Another way is to use Linfo. It is a very fast cross-platform php script that describes the host server in extreme detail, giving information such as ram usage, disk space, raid arrays, hardware, network cards, kernel, os, samba/cups/truecrypt status, temps, disks, and much more.

How to get CPU/RAM usage from differenet servers?

  1. Create a php script that stores the data into a common data storage
    like mysql, redis etc.
  2. Add the php script to cron job to do it in an interval, for that on each server run crontab -e, it will bring you an editor
    window where; put the script interval logic.
  3. modify monitor.php file to get contents from common database.

example:

cron script

  // do.php it runs in every server you have  
$cpu_usage = get_server_cpu_usage();
$memory_usage = get_server_memory_usage();
// you can use hostname, ip name logic
$server_id = get_server_uniqid();
//logic to insert data
insert_monitor_data($server_id, $cpu_usage, $server_id);

crontab file

*/10 * * * * /path/to/do.php

the cron will run every 10 minutes in individual servers & store the data to a common data store.

Checking kubernetes pod CPU and memory without using third party tool

It looks like you want to check memory and CPU usage of the Pods that are Completed.

I tried the k9s tool and I also cannot see CPU, MEM of finished pods, is it normal that we cannot see CPU, MEM of the finished pods ?

I notice it's already running but with this command kubectl top pods -n %namespace% I do see the CPU, MEM of the Running pod but not the one that was already completed.

Pod marked as Completed is no longer running (terminated) and we cannot connect to it with the kubectl exec command:

$ kubectl exec -it -n cronjob hello-1618235100-xwxkc -- bash
error: cannot exec into a container in a completed pod; current phase is Succeeded

We can see the Pod phase using kubectl get -ojson command:

$ kubectl get pod hello-1618235100-xwxkc -n cronjob
NAME READY STATUS RESTARTS AGE
hello-1618235100-xwxkc 0/1 Completed 0 6m11s

$ kubectl get pod hello-1618235100-xwxkc -n cronjob -ojson | grep -i phase
"phase": "Succeeded",

As can be found in the Pod phase documentation:

Succeeded - All containers in the Pod have terminated in success, and will not be restarted.

It is not possible to display that Pods with kubectl top command as the metric server does not store metrics history (see: the Metrics Server documentation documentation):

Only the most recent value of each metric will be remembered. If a user needs an access to historical data they should either use 3rd party monitoring solution or archive the metrics on their own

For example, I use Prometheus + Grafana and have access to historical data of my Pods:
Sample Image

Tracking CPU and Memory usage per process

Press Win+R, type perfmon and press Enter. When the Performance window is open, click on the + sign to add new counters to the graph. The counters are different aspects of how your PC works and are grouped by similarity into groups called "Performance Object".

For your questions, you can choose the "Process", "Memory" and "Processor" performance objects. You then can see these counters in real time

You can also specify the utility to save the performance data for your inspection later. To do this, select "Performance Logs and Alerts" in the left-hand panel. (It's right under the System Monitor console which provides us with the above mentioned counters. If it is not there, click "File" > "Add/remove snap-in", click Add and select "Performance Logs and Alerts" in the list".) From the "Performance Logs and Alerts", create a new monitoring configuration under "Counter Logs". Then you can add the counters, specify the sampling rate, the log format (binary or plain text) and log location.

windows system command to find out cpu/ram usage for the machine+specific app/webapp

Look at tasklist to find the process ID of a process.

Look at WMIC for CPU usage for the system and specific apps.

Combined with find or findstr, should give you all the info you need.

How to determine current memory usage of the pod in k8s inside the python program?

The easiest way to check memory utilization is to use psutil library.

I created a simple kubernetes pod which is using python image. Then, I got a shell to it, using kubectl exec command, and I installed psutil library...

user@shell:~/python-testing $ kubectl exec -it my-test-6d4468964d-67jlx -c my-python -- sh
# pip3 install psutil
Collecting psutil
Downloading psutil-5.8.0.tar.gz (470 kB)
|████████████████████████████████| 470 kB 4.6 MB/s
Building wheels for collected packages: psutil
Building wheel for psutil (setup.py) ... done
Created wheel for psutil: filename=psutil-5.8.0-cp310-cp310-linux_x86_64.whl size=279671 sha256=510df7c715b76b8d7534b72f5fca306cfd1190b59ed318bb77cfa7a4ff76acc8
Stored in directory: /root/.cache/pip/wheels/12/a3/6d/615295409067d58a62a069d30d296d61d3ac132605e3a9555c
Successfully built psutil
Installing collected packages: psutil
Successfully installed psutil-5.8.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.

...and I run following commands to check memory utilization:

# python3 
Python 3.10.0 (default, Nov 17 2021, 15:26:39) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.virtual_memory().percent
16.0

After that I decided to generate some memory usage (about 70%):

>>> psutil.virtual_memory().percent
72.1

So it seems that it is working correctly.

It also works fine when one pod has multiple containers as they are sharing common memory, e.g. one container is consuming 70% of the total memory of the pod, so on the second container I can see that memory utilization is ~ 70%.

To sum up, you need to include something like this in your python code:

import psutil

memory_usage = psutil.virtual_memory().percent
print(memory_usage)

Check also more examples of the psutil commands in this answer.



Related Topics



Leave a reply



Submit