Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ICON_CACHE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/simple_cpu_monitor_icon_cache"
- mkdir -p "$(dirname "$ICON_CACHE_FILE")"
- build_icon_cache() {
- local cache_age=0
- local newest_desktop=0
- if [[ -f "$ICON_CACHE_FILE" ]]; then
- cache_age=$(stat -c %Y "$ICON_CACHE_FILE")
- newest_desktop=$(find ~/.local/share/applications /usr/share/applications -name '*.desktop' -printf '%T@\n' 2>/dev/null | sort -nr | head -n1 | cut -d. -f1)
- fi
- if [[ ! -f "$ICON_CACHE_FILE" || $newest_desktop -gt $cache_age ]]; then
- > "$ICON_CACHE_FILE"
- find ~/.local/share/applications /usr/share/applications -name '*.desktop' -print0 2>/dev/null | while IFS= read -r -d '' file; do
- exec_line=$(grep -m1 '^Exec=' "$file" | cut -d= -f2 | awk '{print $1}')
- icon_line=$(grep -m1 '^Icon=' "$file" | cut -d= -f2)
- if [[ -n "$exec_line" && -n "$icon_line" ]]; then
- echo "$exec_line|$icon_line" >> "$ICON_CACHE_FILE"
- exec_base=$(basename "$exec_line")
- if [[ "$exec_base" != "$exec_line" ]]; then
- echo "$exec_base|$icon_line" >> "$ICON_CACHE_FILE"
- fi
- fi
- done
- fi
- }
- get_icon_for_process() {
- local proc="$1"
- [[ "$proc" == "chrome" ]] && proc="google-chrome-stable"
- local icon
- icon=$(grep -m1 "^$proc|" "$ICON_CACHE_FILE" | cut -d'|' -f2)
- if [[ -n "$icon" ]]; then
- echo "$icon"
- else
- echo "$proc"
- fi
- }
- build_icon_cache
- if [[ "$1" == "top_hungry_processes" ]]; then
- tooltip_opt_n_procs=$(grep -m1 '^maxprocesses_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)
- tooltip_opt_user_only="$(grep -m1 '^userprocessesonly_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
- tooltop_opt_high_cpu="$(grep -m1 '^highcpu_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
- tooltip_opt_cpu_alert=$(grep -m1 '^cpu_alert_value_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)
- tooltip_opt_show_mem_procs="$(grep -m1 '^showmem_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
- tooltop_opt_high_mem="$(grep -m1 '^highmem_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
- tooltip_processes_icons_on_off="$(grep -m1 '^processesIcons=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
- tooltip_opt_mem_alert=$(grep -m1 '^mem_alert_value_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)
- tooltip_opt_hide_no_cpu_use_procs="$(grep -m1 '^hidezerocpu_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
- if [[ -z $tooltip_opt_n_procs ]]; then
- tooltip_opt_n_procs=5
- fi
- if [[ -z $tooltip_opt_mem_alert ]]; then
- tooltip_opt_mem_alert=650
- fi
- if [[ -z $tooltip_opt_cpu_alert ]]; then
- tooltip_opt_cpu_alert=20
- fi
- print_with_icon() {
- local comm="$1"
- local rest="$2"
- if [[ -n "$tooltip_processes_icons_on_off" ]]; then
- echo "icon_off | $rest"
- else
- local icon
- icon=$(get_icon_for_process "$comm")
- echo "$icon | $rest"
- fi
- }
- if [[ -z "$tooltip_opt_user_only" && -n "$tooltop_opt_high_cpu" && -n "$tooltip_opt_show_mem_procs" ]]; then
- ps -u "$USER" -o pid,comm,%cpu,rss --no-headers --sort=-%cpu | \
- awk -v mem_alert="$tooltip_opt_mem_alert" -v cpu_alert="$tooltip_opt_cpu_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" -v high_mem_flag="$tooltop_opt_high_mem" '
- $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
- mem_mb = int($4/1024);
- icon_cpu = ($3 > cpu_alert) ? "đ§ " : "";
- icon_mem = (high_mem_flag && mem_mb > mem_alert) ? "đž" : "";
- if(icon_mem != "" && icon_cpu != "") {
- printf "%s|%s (%s%d%% - %s%dMB)\n", $2, $2, icon_cpu, int($3), icon_mem, mem_mb
- } else if(icon_mem != "") {
- printf "%s|%s (%d%% - %s%dMB)\n", $2, $2, int($3), icon_mem, mem_mb
- } else if(icon_cpu != "") {
- printf "%s|%s (%s%d%% - %dMB)\n", $2, $2, icon_cpu, int($3), mem_mb
- } else {
- printf "%s|%s (%d%% - %dMB)\n", $2, $2, int($3), mem_mb
- }
- }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
- exit
- fi
- if [[ -z "$tooltip_opt_user_only" && -n "$tooltop_opt_high_cpu" && -z "$tooltip_opt_show_mem_procs" ]]; then
- ps -u "$USER" -o pid,comm,%cpu --no-headers --sort=-%cpu | \
- awk -v cpu_alert="$tooltip_opt_cpu_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" '
- $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
- icon = ($3 > cpu_alert) ? "đ§ " : "";
- if(icon != "") {
- printf "%s|%s (%s%d%%)\n", $2, $2, icon, int($3)
- } else {
- printf "%s|%s (%d%%)\n", $2, $2, int($3)
- }
- }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
- exit
- fi
- if [[ -z "$tooltip_opt_user_only" && -z "$tooltop_opt_high_cpu" && -n "$tooltip_opt_show_mem_procs" ]]; then
- ps -u "$USER" -o pid,comm,%cpu,rss --no-headers --sort=-%cpu | \
- awk -v mem_alert="$tooltip_opt_mem_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" -v high_mem_flag="$tooltop_opt_high_mem" '
- $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
- mem_mb = int($4/1024);
- icon_mem = (high_mem_flag && mem_mb > mem_alert) ? "đž" : "";
- if(icon_mem != "") {
- printf "%s|%s (%d%% - %s%dMB)\n", $2, $2, int($3), icon_mem, mem_mb
- } else {
- printf "%s|%s (%d%% - %dMB)\n", $2, $2, int($3), mem_mb
- }
- }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
- exit
- fi
- if [[ -z "$tooltip_opt_user_only" && -z "$tooltop_opt_high_cpu" && -z "$tooltip_opt_show_mem_procs" ]]; then
- ps -u "$USER" -o pid,comm,%cpu --no-headers --sort=-%cpu | \
- awk -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" '
- $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
- printf "%s|%s (%d%%)\n", $2, $2, int($3)
- }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
- exit
- fi
- if [[ -n "$tooltip_opt_user_only" && -n "$tooltop_opt_high_cpu" && -n "$tooltip_opt_show_mem_procs" ]]; then
- ps -eo pid,comm,%cpu,rss --no-headers --sort=-%cpu | \
- awk -v mem_alert="$tooltip_opt_mem_alert" -v cpu_alert="$tooltip_opt_cpu_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" -v high_mem_flag="$tooltop_opt_high_mem" '
- $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
- mem_mb = int($4/1024);
- icon_cpu = ($3 > cpu_alert) ? "đ§ " : "";
- icon_mem = (high_mem_flag && mem_mb > mem_alert) ? "đž" : "";
- if(icon_mem != "" && icon_cpu != "") {
- printf "%s|%s (%s%d%% - %s%dMB)\n", $2, $2, icon_cpu, int($3), icon_mem, mem_mb
- } else if(icon_mem != "") {
- printf "%s|%s (%d%% - %s%dMB)\n", $2, $2, int($3), icon_mem, mem_mb
- } else if(icon_cpu != "") {
- printf "%s|%s (%s%d%% - %dMB)\n", $2, $2, icon_cpu, int($3), mem_mb
- } else {
- printf "%s|%s (%d%% - %dMB)\n", $2, $2, int($3), mem_mb
- }
- }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
- exit
- fi
- if [[ -n "$tooltip_opt_user_only" && -n "$tooltop_opt_high_cpu" && -z "$tooltip_opt_show_mem_procs" ]]; then
- ps -eo pid,comm,%cpu --no-headers --sort=-%cpu | \
- awk -v cpu_alert="$tooltip_opt_cpu_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" '
- $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
- icon = ($3 > cpu_alert) ? "đ§ " : "";
- if(icon != "") {
- printf "%s|%s (%s%d%%)\n", $2, $2, icon, int($3)
- } else {
- printf "%s|%s (%d%%)\n", $2, $2, int($3)
- }
- }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
- exit
- fi
- if [[ -n "$tooltip_opt_user_only" && -z "$tooltop_opt_high_cpu" && -n "$tooltip_opt_show_mem_procs" ]]; then
- ps -eo pid,comm,%cpu,rss --no-headers --sort=-%cpu | \
- awk -v mem_alert="$tooltip_opt_mem_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" -v high_mem_flag="$tooltop_opt_high_mem" '
- $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
- mem_mb = int($4/1024);
- icon_mem = (high_mem_flag && mem_mb > mem_alert) ? "đž" : "";
- if(icon_mem != "") {
- printf "%s|%s (%d%% - %s%dMB)\n", $2, $2, int($3), icon_mem, mem_mb
- } else {
- printf "%s|%s (%d%% - %dMB)\n", $2, $2, int($3), mem_mb
- }
- }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
- exit
- fi
- if [[ -n "$tooltip_opt_user_only" && -z "$tooltop_opt_high_cpu" && -z "$tooltip_opt_show_mem_procs" ]]; then
- ps -eo pid,comm,%cpu --no-headers --sort=-%cpu | \
- awk -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" '
- $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
- printf "%s|%s (%d%%)\n", $2, $2, int($3)
- }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
- exit
- fi
- fi
- get_cpu_values() {
- read -r _ user nice system idle iowait irq softirq steal guest guest_nice < /proc/stat
- total=$((user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice))
- echo "$idle $total"
- }
- read idle1 total1 < <(get_cpu_values)
- sleep 1
- read idle2 total2 < <(get_cpu_values)
- delta_idle=$((idle2 - idle1))
- delta_total=$((total2 - total1))
- cpu_usage=$((100 * (delta_total - delta_idle) / delta_total))
- if ((cpu_usage > 90)); then
- echo -e " \e[91mī ${cpu_usage}%\e[0m"
- else
- echo "ī ${cpu_usage}%"
- fi
Advertisement
Add Comment
Please, Sign In to add comment