summaryrefslogtreecommitdiffhomepage
path: root/monitor.hs
blob: d6f0e34a41863aabf7cc268309dd8e07fb698c80 (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
#! /usr/bin/env runhaskell


import Data.Time (getZonedTime)
import Text.Printf (printf)
import System.Process (runInteractiveCommand)
import Data.List
import System.IO (hGetContents)


getOutput :: String -> IO String
getOutput cmd = do
    (_, out, _, _) <- runInteractiveCommand cmd
    hGetContents out 

memParse :: String -> String
memParse file = 
    let content = map words $ take 4 $ lines file
        [total, free, buffer, cache] = map (\line -> (read $ line !! 1 :: Float) / 1024) content
        rest = free + buffer + cache
        used = total - rest
        usedratio = used * 100 / total
    in
        printf "MEM: %sM %.1f%% used   %.0fM rest" used usedratio rest


mem :: IO String
mem = do
    file <- readFile "/proc/meminfo"
    return $ memParse file


time :: IO String
time = do
    now <- getZonedTime
    return $ take 16 $ show now


temp :: IO String
temp = do
    file <- readFile "/proc/acpi/thermal_zone/THRM/temperature"
    let t = (words file) !! 1
    return $ "TEMP: " ++ t ++ "C"


takeTail :: Int -> [a] -> [a]
takeTail n xs =
    let len = length xs in
        drop (len-n) xs


load :: IO String
load = do
    content <- getOutput "uptime"
    let l = map (delete ',') $ takeTail 3 $ words content
    return $ unwords $ "LOAD:" : l


sep :: IO String
sep = return "        "


main = do 
    putStr ""
    mapM_ (>>=putStr) $ intersperse sep [load, temp, mem, time]
    putChar '\n'