Sponsored Content
Operating Systems Linux Gentoo cpu%/mem% usage, scripting, dzen2: howto learn bash the hard way Post 302223761 by broli on Monday 11th of August 2008 10:20:55 AM
Old 08-11-2008
Quote:
Originally Posted by era
Something like this maybe.

Code:
#!/bin/sh

while :
do
    top -b -n -1 |
    awk 'NR > 5 { mem[$12] = $10; }
        NR==3 { printf "^fg(green)CPU " $5 " ** "; next; }
        NR==4 { total=$2; free=$6; buffers=$8; next; }
        NR==5 { cached=$8; next; }
        NR==8, NR==12 { printf "[^fg(cyan)" $12 "(^fg(red)" $9 "^fg(green)]--"; next; }
        NR==13 { printf ">>\n"; next; }
        END { ... sort and extract top 4 items from mem array here ...;
            printf "%2.2f\n", (free+buffers+cached)/total*100 }'
    sleep 4
done | dzen2 --options

I haven't finished the mem part but it's not too complex; you should be able to implement the remaining part of the awk script and sort and pick the output lines you want by searching the forums a bit. The mawk manual page has an example of how to implement a simple sort in awk.

My top prints "Mem: total used free buffers" followed by "Swap: total used free cached" on lines 4 and 5; I'm not entirely sure which of the "free" and "total" fields you want, but extracting the fields you want (provided you are sure which ones you want) should be similarly straightforward. I've put in placeholders for those. A complication is that the output has a human-readable suffix like "k" which I imagine might vary, so properly you should parse that before doing the math on the resulting numbers.
yeah, this thing is growing faster than my speed to read awk info :P
i will look at that and run some tests
the problem, is that all this work only o get a simple percentage of free mem, is growing in complexity and footprint
to a point where i dont know if its worth the problem
maybe i can make use of an external program to give me that info.
maybe doit myself in C? .... (actually, stole the related code of wmmem )
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Difference in Mem usage ?

Hi All, I have a pair of sun ultra 5_10 with SunOS 5.5.1. Both are almost equally patched and set up with simillar applications. host# uname -a SunOS host 5.5.1 Generic_103640-24 sun4u sparc SUNW,Ultra-5_10 Even though both have same amount of RAM ( 512 Mb ) , ... (1 Reply)
Discussion started by: shibz
1 Replies

2. Linux

High Mem & Cpu Utilisation

Hi All, Kindly help me in optimizing the server as it displays a great amount of CPU & MEM being utilised when the mysql process executes. Below are the stats --- -------------------------------------------------------------------------- # top 15:51:57 up 23:22, 5 users, load average:... (1 Reply)
Discussion started by: gautamatul82
1 Replies

3. Linux

Linux Mem Usage

What is amount of free RAM i have now? total used free shared buffers cached Mem: 1010 963 46 0 215 256 -/+ buffers/cache: 491 518 Swap: 1983 0 1983 Above is the output of... (1 Reply)
Discussion started by: new2ss
1 Replies

4. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

5. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

6. Shell Programming and Scripting

Help with bash script - Need to get CPU usage as a percentage

I'm writing a bash script to log some selections from a sensors output (core temp, mb temp, etc.) and I would also like to have the current cpu usage as a percentage. I have no idea how to go about getting it in a form that a bash script can use. For example, I would simply look in the output of... (3 Replies)
Discussion started by: graysky
3 Replies

7. UNIX for Dummies Questions & Answers

HOWTO - Total memory and CPU usage ... without top?

Hi all, Is it possible to get total memory usage and free memory usage without top? By Googling I found for total memory usage, use vmstat, for CPU, use mpstat, for disk I/O use iostat, is this correct? Will using sar gives the same result as ALL of these three (3) commands? What about if I... (2 Replies)
Discussion started by: newbie_01
2 Replies

8. Shell Programming and Scripting

Should I learn bash scripting or is it going obsolete?

Hi folks, I'm a CS students enrolled in a sysadmin class where we've been covering bash scripting for the past few weeks. I have prior knowledge in java, c++, c#, python,and some scripting languages like asp.net w/c# and php. This bash stuff seems pretty neat and a bit different than what I am... (9 Replies)
Discussion started by: KalEl
9 Replies

9. Shell Programming and Scripting

Command to find total cpu and mem

Hi All, I need to know the command for knowing the total cpu and mem. Thanks (5 Replies)
Discussion started by: aish11
5 Replies

10. UNIX for Dummies Questions & Answers

Learn bash shell scripting

I do not know shell scripting. But at work place, I have got an in and out shell scripting task. I just need to understand a very big script. Is there any tool in which I can place the script and it can tell me the meaning of the whole script? (3 Replies)
Discussion started by: lg123
3 Replies
I3STATUS(1)							     i3 Manual							       I3STATUS(1)

NAME
i3status - Generates a status line for dzen2 or xmobar SYNOPSIS
i3status [-c configfile] [-h] [-v] OPTIONS
-c Specifies an alternate configuration file path. By default, i3status looks for configuration files in the following order: 1. ~/.i3status.conf 2. ~/.config/i3status/config (or $XDG_CONFIG_HOME/i3status/config if set) 3. /etc/i3status.conf 4. /etc/xdg/i3status/config (or $XDG_CONFIG_DIRS/i3status/config if set) DESCRIPTION
i3status is a small program (about 1500 SLOC) for generating a status bar for i3bar, dzen2, xmobar or similar programs. It is designed to be very efficient by issuing a very small number of system calls, as one generally wants to update such a status line every second. This ensures that even under high load, your status bar is updated correctly. Also, it saves a bit of energy by not hogging your CPU as much as spawning the corresponding amount of shell commands would. CONFIGURATION
The basic idea of i3status is that you can specify which "modules" should be used (the order directive). You can then configure each module with its own section. For every module, you can specify the output format. See below for a complete reference. Sample configuration. general { output_format = "dzen2" colors = true interval = 5 } order += "ipv6" order += "disk /" order += "run_watch DHCP" order += "run_watch VPN" order += "wireless wlan0" order += "ethernet eth0" order += "battery 0" order += "cpu_temperature 0" order += "load" order += "time" wireless wlan0 { format_up = "W: (%quality at %essid, %bitrate) %ip" format_down = "W: down" } ethernet eth0 { # if you use %speed, i3status requires the cap_net_admin capability format_up = "E: %ip (%speed)" format_down = "E: down" } battery 0 { format = "%status %percentage %remaining %emptytime" path = "/sys/class/power_supply/BAT%d/uevent" } run_watch DHCP { pidfile = "/var/run/dhclient*.pid" } run_watch VPN { pidfile = "/var/run/vpnc/pid" } time { format = "%Y-%m-%d %H:%M:%S" } load { format = "%5min" } cpu_temperature 0 { format = "T: %degrees oC" path = "/sys/devices/platform/coretemp.0/temp1_input" } disk "/" { format = "%free" } General The colors directive will disable all colors if you set it to false. You can also specify the colors that will be used to display "good", "degraded" or "bad" values using the color_good, color_degraded or color_bad directives, respectively. Those directives are only used if color support is not disabled by the colors directive. The input format for color values is the canonical RGB hexadecimal triplet (with no separators between the colors), prefixed by a hash character ("#"). Example configuration: color_good = "#00FF00" Likewise, you can use the color_separator directive to specify the color that will be used to paint the separator bar. The separator is always output in color, even when colors are disabled by the colors directive. The interval directive specifies the time in seconds for which i3status will sleep before printing the next status line. Using output_format you can chose which format strings i3status should use in its output. Currently available are: i3bar i3bar comes with i3 and provides a workspace bar which does the right thing in multi-monitor situations. It also comes with tray support and can display the i3status output. This output type uses JSON to pass as much meta-information to i3bar as possible (like colors, which blocks can be shortened in which way, etc.). dzen2 Dzen is a general purpose messaging, notification and menuing program for X11. It was designed to be scriptable in any language and integrate well with window managers like dwm, wmii and xmonad though it will work with any windowmanger xmobar xmobar is a minimalistic, text based, status bar. It was designed to work with the xmonad Window Manager. none Does not use any color codes. Separates values by the pipe symbol. This should be used with i3bar and can be used for custom scripts. IPv6 This module gets the IPv6 address used for outgoing connections (that is, the best available public IPv6 address on your computer). Example format_up: %ip Example format_down no IPv6 Disk Gets used, free, available and total amount of bytes on the given mounted filesystem. Example order: disk /mnt/usbstick Example format: %free (%avail)/ %total Run-watch Expands the given path to a pidfile and checks if the process ID found inside is valid (that is, if the process is running). You can use this to check if a specific application, such as a VPN client or your DHCP client is running. Example order: run_watch DHCP Example format: %title: %status Wireless Gets the link quality and ESSID of the given wireless network interface. You can specify different format strings for the network being connected or not connected. Example order: wireless wlan0 Example format: W: (%quality at %essid, %bitrate) %ip Ethernet Gets the IP address and (if possible) the link speed of the given ethernet interface. Getting the link speed requires the cap_net_admin capability. Set it using setcap cap_net_admin=ep $(which i3status). Example order: ethernet eth0 Example format: E: %ip (%speed) Battery Gets the status (charging, discharging, running), percentage and remaining time of the given battery and when it's estimated to be empty. If you want to use the last full capacity instead of the design capacity (when using the design capacity, it may happen that your battery is at 23% when fully charged because it's old. In general, I want to see it this way, because it tells me how worn off my battery is.), just specify last_full_capacity = true. If your battery is represented in a non-standard path in /sys, be sure to modify the "path" property accordingly. The first occurence of %d gets replaced with the battery number, but you can just hard-code a path as well. Example order: battery 0 Example format: %status %remaining (%emptytime) CPU-Temperature Gets the temperature of the given thermal zone. Example order: cpu_temperature 0 Example format: T: %degrees oC CPU Usage Gets the percentual CPU usage from /proc/stat (Linux) or sysctl(3) (FreeBSD/OpenBSD). Example order: cpu_usage Example format: %usage Load Gets the system load (number of processes waiting for CPU time in the last 1, 5 and 15 minutes). Example order: load Example format: %1min %5min %15min Time Formats the current system time. See strftime(3) for the format. Example order: time Example format: %Y-%m-%d %H:%M:%S DDate Outputs the current discordian date in user-specified format. See ddate(1) for details on the format string. Note: Neither %. nor %X are implemented yet. Example order: ddate Example format: %{%a, %b %d%}, %Y%N - %H Volume Outputs the volume of the specified mixer on the specified device. Works only on Linux because it uses ALSA. A simplified configuration can be used on FreeBSD and OpenBSD due to the lack of ALSA, the device, mixer and mixder_idx options can be ignored on these systems. On these systems the OSS API is used instead to query /dev/mixer directly. Example order: volume master Example format: : %volume Example configuration: volume master { format = ": %volume" device = "default" mixer = "Master" mixer_idx = 0 } USING I3STATUS WITH DZEN2 After installing dzen2, you can directly use it with i3status. Just ensure that output_format is set to dzen2. Example for usage of i3status with dzen2: i3status | dzen2 -fg white -ta r -w 1280 -fn "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1" USING I3STATUS WITH XMOBAR To get xmobar to start, you might need to copy the default configuration file to ~/.xmobarrc. Also, ensure that the output_format option for i3status is set to xmobar. Example for usage of i3status with xmobar: i3status | xmobar -o -t "%StdinReader%" -c "[Run StdinReader]" WHAT ABOUT MEMORY USAGE OR CPU FREQUENCY
? While talking about two specific things, please understand this section as a general explanation why your favorite information is not included in i3status. Let's talk about memory usage specifically. It is hard to measure memory in a way which is accurate or meaningful. An in-depth understanding of how paging and virtual memory work in your operating system is required. Furthermore, even if we had a well-defined way of displaying memory usage and you would understand it, I think that it's not helpful to repeatedly monitor your memory usage. One reason for that is that I have not run out of memory in the last few years. Memory has become so cheap that even in my 4 year old notebook, I have 8 GiB of RAM. Another reason is that your operating system will do the right thing anyway: Either you have not enough RAM for your workload, but you need to do it anyway, then your operating system will swap. Or you don't have enough RAM and you want to restrict your workload so that it fits, then the operating system will kill the process using too much RAM and you can act accordingly. For CPU frequency, the situation is similar. Many people don't understand how frequency scaling works precisely. The generally recommended CPU frequency governor ("ondemand") changes the CPU frequency far more often than i3status could display it. The display number is therefore often incorrect and doesn't tell you anything useful either. In general, i3status wants to display things which you would look at occasionally anyways, like the current date/time, whether you are connected to a WiFi network or not, and if you have enough disk space to fit that 4.3 GiB download. However, if you need to look at some kind of information more than once in a while (like checking repeatedly how full your RAM is), you are probably better off with a script doing that, which pops up an alert when your RAM usage reaches a certain threshold. After all, the point of computers is not to burden you with additional boring tasks like repeatedly checking a number. EXTERNAL SCRIPTS
/PROGRAMS WITH I3STATUS In i3status, we don't want to implement process management again. Therefore, there is no module to run arbitrary scripts or commands. Instead, you should use your shell, for example like this: Example for prepending the i3status output: #!/bin/sh # shell script to prepend i3status with more stuff i3status | while : do read line echo "mystuff | $line" || exit 1 done Put that in some script, say .bin/my_i3status.sh and execute that instead of i3status. SEE ALSO
strftime(3), date(1), glob(3), dzen2(1), xmobar(1) AUTHORS
Michael Stapelberg and contributors Thorsten Toepper Baptiste Daroussin Axel Wagner Fernando Tarla Cardoso Lemos i3status v2.5 05/11/2012 I3STATUS(1)
All times are GMT -4. The time now is 10:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy