]> arthur.barton.de Git - netdata.git/blob - netdata.start
made it create by default a dashboard of graphs (all graphs on the same
[netdata.git] / netdata.start
1 #!/bin/bash
2
3 base="`dirname "$0"`"
4
5 if [ ! -d "$base" -o ! -f "$base/netdata.c" ]
6 then
7         echo >&2 "Cannot find my home directory '${base}'."
8         exit 1
9 fi
10 cd "$base" || exit 1
11
12 echo "Creating a directory for netdata..."
13 data=
14 for x in /run/netdata /var/run/netdata /tmp/netdata
15 do
16         echo "  Trying '${x}'..."
17         if [ ! -d "${x}" ]
18         then
19                 mkdir "${x}"
20                 if [ $? -eq 0 ]
21                 then
22                         echo "  OK. '${x}' works."
23                         data="${x}"
24                         break
25                 fi
26         else
27                 echo "  OK. '${x}' works."
28                 data="${x}"
29                 break
30         fi
31 done
32
33 if [ -z "${data}" ]
34 then
35         echo >&2 "Cannot find where to put JSON files."
36         exit 1
37 fi
38
39 echo "Stopping a (possibly) running netdata..."
40 killall netdata 2>/dev/null
41
42 echo "Compiling netdata"
43 gcc -O3 -o netdata netdata.c || exit 1
44
45 echo "Starting netdata"
46 ./netdata -d -u 1 -l 60 -o "${data}" || exit 1
47
48 echo "Waiting 2 seconds for the JSON files"
49 # wait 2 seconds for the JSON files to be generated
50 sleep 2
51
52 if [ -h data ]
53 then
54         echo "Removing existing $base/data link"
55         rm data || exit 1
56 fi
57
58 if [ ! -d data ]
59 then
60         echo "Linking '${data}' to $base/data"
61         ln -s "${data}" data || exit 1
62 else
63         echo >&2 "Directory $base/data already exists. Not touching it, however it should be a link '${data}'."
64 fi
65
66 echo "Finding proper parameters for dashboard..."
67
68 all=`ls "${data}" | grep .json$ | sed "s/\.json$//g"`
69 count=0
70 for x in $all
71 do
72         count=$[count + 1]
73 done
74
75 w=1
76 h=1
77 a=1
78 while [ $a -lt $count ]
79 do
80         if [ $[h+1] -le $w ]
81         then
82                 h=$[h+1]
83         else
84                 w=$[w+1]
85                 h=$[h-1]
86         fi
87         a=$[w*h]
88 done
89 if [ $w -gt 10 ]
90 then
91         w=10
92 fi
93 if [ $h -gt 10 ]
94 then
95         h=10
96 fi
97
98 echo "Generating all.html"
99 cat >all.html <<EOF
100 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
101 <html xmlns="http://www.w3.org/1999/xhtml">
102 <style>
103         * {font-family:Arial}
104         div {float: left; margin: 0 0 0 0; }
105 </style>
106 <title>netdata</title>
107 <head>
108         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
109
110         <!--Load the AJAX API-->
111         <script type="text/javascript" src="https://www.google.com/jsapi"></script>
112         <script type="text/javascript" src="jquery-1.10.1.min.js"></script>
113         <script type="text/javascript" src="netdata.js"></script>
114         <script type="text/javascript">
115         
116         // Set a callback to run when the Google Visualization API is loaded.
117         google.setOnLoadCallback(drawCharts);
118
119         function drawCharts() {
120                 var width = $w; // if zero, auto-adjusts to 50% of screen, 1-10 goes 1/width of screen
121                 var height = $h;
122                 
123                 // EDIT: add one line per interface you have
124                 // EDIT: 
125                 // EDIT:   name    div id                    json data           graph title
126                 // EDIT: --------------------------------------------------------------------------------
127 EOF
128
129 for x in $all
130 do
131         cat >>all.html <<EOF2
132                 drawChart('${x}', '${x}_div', width, height, "data/${x}.json", "Live Network Usage for ${x}");
133 EOF2
134 done
135
136 ref=4
137 tim=1000
138 if [ $count -gt 4 ]
139 then
140         ref=1
141         tim=$[1000 / count]
142         if [ $tim -lt 100 ]
143         then
144                 tim=100
145         fi
146 fi
147
148 cat >>all.html <<EOF3
149         }
150         
151         function myChartsRefresh() {
152                 // refresh up to 4 charts per second
153                 refreshCharts($ref);
154         }
155
156         // EDIT: how often the charts are updated, in milliseconds
157         setInterval(myChartsRefresh, $tim);
158         </script>
159 </head>
160
161 <body>
162         <!--
163                 EDIT: add one div per interface you have
164                 EDIT: use the same id above and bellow!
165         -->
166 EOF3
167
168 for x in $all
169 do
170         cat >>all.html <<EOF4
171         <div id="${x}_div"></div>
172 EOF4
173 done
174
175 cat >>all.html <<EOF5
176  </body>
177 </html>
178 EOF5
179
180 echo "All Done."
181 echo "Just hit all.html from your browser."
182