#! /bin/sh

#
# Generate a configuration file for GKrellM 2.3.6
#

#
# Variables
#

GRIDS=5
HEIGHT=25


#
# Functions
#

print_usage () {
  printf "Usage: %s [OPTION]...\n" "$(basename "$0")"
  printf "Generate a configuration file for GKrellM 2.3.6\n"
  printf "\nOPTIONS:\n"
  printf "  %-20s%s\n" "-g GRIDS" "Number of grids (0-5, default 5)"
  printf "  %-20s%s\n" "-H HEIGHT" "Charts height (5-200, default 25)"
  printf "  %-20s%s\n" "-h" "Print this help message"
}

check_int () {
  for STRING in "$@" ; do
    printf "%s" "$STRING" | grep -q '[^[:digit:]]' && return 1
  done
  return 0
}


#
# Options processing
#

while getopts "g:H:h" OPTION ; do
  case $OPTION in
    g) GRIDS="$OPTARG" ;;
    H) HEIGHT="$OPTARG" ;;
    h) print_usage ; exit 0 ;;
    *) print_usage ; exit 1 ;;
  esac
done ; shift $((OPTIND-1))


#
# Checks
#

if ! ([ -n "$GRIDS" ] && check_int $GRIDS && [ $GRIDS -ge 0 ] && [ $GRIDS -le 5 ]) ; then
  printf "GRIDS must be an integer between 0 and 5, using default value (5).\n" 1>&2
  GRIDS=5
fi

if ! ([ -n "$HEIGHT" ] && check_int $HEIGHT && [ $HEIGHT -ge 5 ] && [ $HEIGHT -le 200 ]) ; then
  printf "HEIGHT must be an integer between 5 and 200, using default value (25).\n" 1>&2
  HEIGHT=25
fi


#
# Main
#

# Get the number of CPUs
CPUS=$(grep -c ^processor /proc/cpuinfo)

# Get the names of network interfaces
IFACES="$(ip link | sed -E '/^(1| )/d ; s/^[[:digit:]]+:[[:blank:]]+([^[[:blank:]]+):[[:blank:]]+.*$/\1/')"

# Get mount points
MOUNTS="$(grep -E "^/dev" /proc/mounts | awk '{print $2}' | grep -E -v "^/(dev|media|mnt|tmp)")"

# Go
cat << EOF
### GKrellM user config.  Auto written, do not edit (usually) ###
### Version 2.3.6 ###
enable_hostname 1
hostname_short 0
enable_sysname 1
mbmon_port 0
sticky_state 1
dock_type 0
decorated 0
skip_taskbar 1
skip_pager 1
above 0
below 0
track_gtk_theme_name 0
default_track_theme "Default"
save_position 1
chart_width 96
update_HZ 10
allow_multiple_instances 0
float_factor 1000
hostname sysname_mode 0
clock_cal clock_launch 
clock_cal clock_tooltip 
clock_cal cal_launch 
clock_cal cal_tooltip 
clock_cal hour_chime_command 
clock_cal quarter_chime_command 
clock_cal loop_chime_enable 0
clock_cal clock_options 1
clock_cal cal_options 1
clock_cal cal_format %a <span foreground="\$A"><big><big>%e</big></big></span> %b
clock_cal clock_format %k:%M <span foreground="\$A"><small>%S</small></span>
cpu enable 1
cpu smp_mode 1
cpu enabled cpu 1
cpu launch cpu x-terminal-emulator -T CPU -e htop -s PERCENT_CPU
cpu extra_info cpu 1
cpu chart_config cpu $HEIGHT 20 $GRIDS 0 0 0 : 0 1 0 0 500 : 0 0 0 0 500 : 0 0 0 0 500
$(CPU=0 ; while [ $CPU -lt $CPUS ] ; do
  printf 'cpu enabled cpu%s 0\n' $CPU
  printf 'cpu extra_info cpu%s 1\n' $CPU
  printf 'cpu chart_config cpu%s %s 20 %s 0 0 0 : 0 1 0 0 500 : 0 0 0 0 500 : 0 0 0 0 500\n' $CPU $HEIGHT $GRIDS
  CPU=$((CPU+1))
done)
cpu show_panel_labels 1
cpu omit_nice_mode 0
cpu config_tracking 1
cpu sensor_mode 1
cpu text_format \t\fS \$s\r\$T\b\fU \$u\r\fN \$n
cpu alert_config  values 1 1 0 0
cpu alert_config  repeat 0 0
cpu alert_config  limits 95000 75000 0 0
cpu alert_config  delay 2 3600 2 1
cpu alert_config  id_string cpu
cpu alert_includes_nice 0
proc enable 1 1
proc launch 
proc tooltip_comment 
proc sensor_mode 1
proc text_format \t\fU \$u\rL \$l\b\fP \$p\r\fF \$f
proc chart_config $HEIGHT 100 $GRIDS 0 0 0 : 1 0 0 0 500 : 0 0 0 0 500
proc alert_config load  values 1 1 0 0
proc alert_config load  repeat 0 0
proc alert_config load  limits $(printf '%d*1000\n' $CPUS | bc) $(printf '%d*1000/4*3\n' $CPUS | bc) 0 0
proc alert_config load  delay 0 10000 0 1
proc alert_config load  id_string proc-load
proc alert_config users  values 1 1 0 0
proc alert_config users  repeat 0 0
proc alert_config users  limits 3000 2000 0 0
proc alert_config users  id_string proc-users
disk assign_method 2
disk device Disk 0 0 0 1 1 0 0
disk launch Disk x-terminal-emulator -T Disk -e sudo iotop
disk chart_config Disk $HEIGHT 0 $GRIDS 1 0 0 : 0 1 0 0 500 : 0 0 0 0 500
disk text_format \t\fW \$w\r\$T\b\fR \$r
inet text_format all \t\f\$l \$a\r\$c60\b\f\$L \$A\r\$C60
inet update_interval all 1
$(for IFACE in $IFACES ; do
  printf 'net enables %s 1 1 0\n' $IFACE
  printf 'net chart_config %s %s 0 %s 1 0 0 : 0 1 0 0 500 : 0 0 0 0 500\n' $IFACE $HEIGHT $GRIDS
  printf 'net launch %s x-terminal-emulator -T %s -e sudo iftop -i %s -B -P\n' $IFACE $IFACE $IFACE
done)
net enables lo 0 1 0
net timer_enabled 0
net timer_seconds 1
net timer_iface none
net timer_on 
net timer_off 
net text_format \t\fT \$t\r\$T\b\fR \$r\c\f\$L
net reset_mday 1
net net_enabled_as_default 1
net net_stats_window_height 200
meminfo mem_meter 1 1 1
$(if grep -q -v "^Filename" /proc/swaps ; then
  SWAP_ENABLED=1
else
  SWAP_ENABLED=0
fi
printf "meminfo swap_meter %i 1\n" $SWAP_ENABLED)
meminfo swap_chart 0 1
meminfo chart_config $HEIGHT 0 $GRIDS 1 0 0 : 0 1 0 0 500 : 0 0 0 0 500
meminfo mem_launch x-terminal-emulator -T Memory -e htop -s PERCENT_MEM
meminfo mem_tooltip 
meminfo mem_data_format Mem: \$u
meminfo swap_launch 
meminfo swap_tooltip 
meminfo swap_data_format Swp: \$u
meminfo text_format \t\fO \$o\r\$T\b\fI \$i
meminfo alert_config mem  values 1 1 0 0
meminfo alert_config mem  repeat 0 0
meminfo alert_config mem  limits 95000 75000 0 0
meminfo alert_config mem  id_string meminfo-mem
meminfo alert_config swap  values 1 1 0 0
meminfo alert_config swap  repeat 0 0
meminfo alert_config swap  limits 10000 10000 0 0
meminfo alert_config swap  id_string meminfo-swap
$(for MOUNT in $MOUNTS ; do
  NAME="$(printf "%s" "$MOUNT" | sed -e 's/^\///' -e 's/^$/root/' -e 's/^./\u&/')"
  printf 'fs "%s" "%s" 0 0 0 1 0\n' "$NAME" "$MOUNT"
  printf 'fs alert_config "%s"  values 1 1 0 0\n' "$NAME"
  printf 'fs alert_config "%s"  repeat 0 0\n' "$NAME"
  printf 'fs alert_config "%s"  limits 95000 90000 0 0\n' "$NAME"
  printf 'fs alert_config "%s"  id_string fs-%s\n' "$NAME" "$NAME"
done)
fs fs_check_timeout 2
fs nfs_check_timeout 16
fs auto_eject 0
fs binary_units 1
fs data_format \$l \$f free
mail animation_select_mode 1
mail enable 0 0 0 0
battery enable 1
battery enable_composite 1
battery estimate_time 0
battery estimate_time_discharge 1500
battery estimate_time_charge 3000
battery estimate_time_charge_model 0
battery full_cap_fallback 5000
battery poll_interval 5
battery alert_units_percent 1
battery alert_config  values 1 0 1 0
battery alert_config  repeat 0 0
battery alert_config  limits 0 0 10000 5000
battery alert_config  id_string battery
uptime enable 1
uptime launch 
uptime tooltip 
EOF
