aboutsummaryrefslogtreecommitdiff
path: root/.i3-blocks/blocks/memory
blob: 866e7c31e6599e0c6d3dde9c96b2896ce5b4fc0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash

# First argument is source (mem/swap), second is output value
# Check /proc/meminfo for possible instances
INSTANCE="${BLOCK_INSTANCE:-mem;free}"

SOURCE=$(echo "${INSTANCE}" | awk -F ';' '{print $1}')
DISPLAY=$(echo "${INSTANCE}" | awk -F ';' '{print $2}')

if [[ "${SOURCE}" = "mem" ]]; then
  URGENT_VALUE=90
elif [[ "${SOURCE}" = "swap" ]]; then
  URGENT_VALUE=50
fi

if [[ "${DISPLAY}" = "" ]]; then
  DISPLAY="free"
fi

ONE_KB=1024
ONE_MB=$(echo "${ONE_KB}*1024" | bc -l)
ONE_GB=$(echo "${ONE_MB}*1024" | bc -l)
ONE_TB=$(echo "${ONE_GB}*1024" | bc -l)

# Grep the value and remove KB so we can calculate with it later
#MEMINFO=$(cat /proc/meminfo | grep "${INSTANCE}" | awk -F ':' '{print $2}' | tr -d ' kB')
MEMORY_INFOS=$(cat /proc/meminfo)
SOURCE_TOTAL=$(echo "${MEMORY_INFOS}" | grep -i "${SOURCE}total" | awk -F ':' '{print $2}' | tr -d ' kB')

if [[ "${SOURCE_TOTAL}" -le 0 ]]; then
  exit
fi

SOURCE_FREE=$(echo "${MEMORY_INFOS}" | grep -i "${SOURCE}available" | awk -F ':' '{print $2}' | tr -d ' kB')
SOURCE_USED=$(echo "scale=0; ${SOURCE_TOTAL}-${SOURCE_FREE}" | bc -l)
SOURCE_PERC=$(echo "scale=0; (${SOURCE_USED}*100)/${SOURCE_TOTAL}" | bc -l)

if [[ "${DISPLAY}" = "free" ]]; then
  MEMINFO="${SOURCE_FREE}"
elif [[ "${DISPLAY}" = "used" ]]; then
  MEMINFO="${SOURCE_USED}"
elif [[ "${DISPLAY}" = "total" ]]; then
  MEMINFO="${SOURCE_TOTAL}"
elif [[ "${DISPLAY}" = "perc" ]]; then
  MEMINFO="${SOURCE_PERC}%"
fi

if [[ "${DISPLAY}" != "perc" ]]; then
  # Convert KB meminfo to bytes
  MEMINFO=$(echo "${MEMINFO}*${ONE_KB}" | bc -l)

  if [[ "${MEMINFO}" -ge "${ONE_TB}" ]]; then
    MEMINFO=$(echo "scale=3;${MEMINFO}/${ONE_TB}" | bc -l)"tb"
  elif [[ "${MEMINFO}" -ge "${ONE_GB}" ]]; then
    MEMINFO=$(echo "scale=2;${MEMINFO}/${ONE_GB}" | bc -l)"gb"
  elif [[ "${MEMINFO}" -ge "${ONE_MB}" ]]; then
    MEMINFO=$(echo "scale=1;${MEMINFO}/${ONE_MB}" | bc -l)"mb"
  else
    MEMINFO=$(echo "scale=0;${MEMINFO}/${ONE_KB}" | bc -l)"kb"
  fi
fi

echo "${MEMINFO}"
echo "${MEMINFO}"
echo ""

if [[ "${SOURCE_PERC}" -gt "${URGENT_VALUE}" ]]; then
  exit 33
fi