summaryrefslogtreecommitdiffhomepage
path: root/monitor.hs
blob: 7705bc6d31f4384367ec9b8a2159c08cf42fd649 (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
70
#! /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
        realused = if usedratio > 50 then "^#FF0000"++show used++"^#FFFFFF"
                   else "^#FF00FF"++show used++"^#FFFFFF"
    in
        printf "MEM: %sM %.1f%% used   %.0fM rest" realused 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
        f t | read t > 60 = "^#FF0000"++t++"^#FFFFFF"
            | otherwise = "^#00FF00"++t++"^#FFFFFF"
    return $ "TEMP: " ++ (f 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'