]> arthur.barton.de Git - netdata.git/blob - netdata.start
code cleanup
[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 # every how many seconds to show the graphs
13 NETDATA_CONFIG_UPDATE_EVERY=2
14
15 # the detail of the data that will be kept in netdata
16 NETDATA_CONFIG_INTERNAL_UPDATE_EVERY=1
17
18 # how many points should the graphs have
19 NETDATA_CONFIG_HISTORY_POINTS=120
20
21 # how many points should be kept in memory
22 NETDATA_CONFIG_HISTORY_DATA=3600
23
24 # every how many graph refreshes, the page should be reloaded
25 # this fixes a bug in the google graphs API which leaks memory
26 # when refreshes graphs
27 NETDATA_CONFIG_RELOAD_EVERY=500
28
29 if [ -f netdata.conf ]
30 then
31         source netdata.conf
32 fi
33
34 # how many history lines to keep in netdata
35 NETDATA_HISTORY_LINES=$[NETDATA_CONFIG_UPDATE_EVERY * NETDATA_CONFIG_HISTORY_POINTS / NETDATA_CONFIG_INTERNAL_UPDATE_EVERY]
36
37 echo "Creating a directory for netdata..."
38 data=
39 for x in /run/netdata /var/run/netdata /tmp/netdata
40 do
41         echo "  Trying '${x}'..."
42         if [ ! -d "${x}" ]
43         then
44                 mkdir "${x}" 2>/dev/null
45                 if [ $? -eq 0 ]
46                 then
47                         echo "  OK. '${x}' works."
48                         data="${x}"
49                         break
50                 fi
51         else
52                 echo "  OK. '${x}' works."
53                 data="${x}"
54                 break
55         fi
56 done
57
58 if [ -z "${data}" ]
59 then
60         echo >&2 "Cannot find where to put netdata files."
61         exit 1
62 fi
63
64 if [ -h data ]
65 then
66         echo "Removing existing $base/data link"
67         rm data || exit 1
68 fi
69
70 if [ ! -d data ]
71 then
72         echo "Linking '${data}' to $base/data"
73         ln -s "${data}" data || exit 1
74 else
75         echo >&2 "Directory $base/data already exists. Not touching it, however it should be a link '${data}'."
76 fi
77
78 cp "${base}/all.xsl" "${data}/"
79 cp "${base}/netdata.js" "${data}/"
80 cp "${base}/tc-all.sh" "${data}/"
81 chmod 700 "${data}/tc-all.sh"
82
83 echo "Finding proper parameters for dashboard..."
84
85 echo "Stopping a (possibly) running netdata..."
86 killall netdata 2>/dev/null
87 killall tc-all.sh 2>/dev/null
88
89 sleep 2
90
91 echo "Compiling netdata"
92 # gcc -Wall -O3 -o netdata netdata.c -lpthread || exit 1
93 gcc -Wall -ggdb -o netdata netdata.c -lpthread || exit 1
94
95 echo "Starting netdata"
96 p=`pwd`
97 cd data
98 ulimit -c unlimited
99 $p/netdata -d -u $NETDATA_CONFIG_INTERNAL_UPDATE_EVERY -l $NETDATA_HISTORY_LINES || exit 1
100 cd "$p"
101
102 sleep 2
103
104 # count all graphs
105 all=`wget http://127.0.0.1:19999/list -O - 2>/dev/null`
106 count=0
107 for x in $all
108 do
109         y=`echo "$x" | tr ".-" "__"`
110         eval "t=\${NETDATA_PRIORITY_${y}}"
111         if [ ! "$t" = "IGNORE" ]
112         then
113                 count=$[count + 1]
114         fi
115 done
116
117 echo "We have $count graphs..."
118
119 echo "Generating ${data}/index.html"
120 host="`hostname`"
121 cat >${data}/index.html <<EOF
122 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
123 <html xmlns="http://www.w3.org/1999/xhtml">
124 <style>
125         * {font-family:Arial}
126         div {float: left; margin: 0 0 0 0; }
127 </style>
128 <title>${host} netdata</title>
129 <head>
130         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
131
132         <!--Load the AJAX API-->
133         <script type="text/javascript" src="https://www.google.com/jsapi"></script>
134         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
135         <script type="text/javascript" src="/file/netdata.js"></script>
136         <script type="text/javascript">
137         
138         // Set a callback to run when the Google Visualization API is loaded.
139         google.setOnLoadCallback(drawCharts);
140         
141         function drawCharts() {
142                 // EDIT: add one line per interface you have
143                 // EDIT: 
144                 // EDIT:   name    div id                    json data           graph                                  vertical axis title
145                 // EDIT: --------------------------------------------------------------------------------------------------
146 EOF
147
148 for x in $all
149 do
150         y=`echo "$x" | tr ".-" "__"`
151
152         title=
153         vtitle=
154         case "$x" in
155                 net.*)
156                         title="Network Usage for "
157                         vtitle="bandwidth in kilobits/s"
158                         ;;
159
160                 disk.*)
161                         title="Disk I/O for "
162                         vtitle="I/O in kilobytes/s"
163                         ;;
164
165                 tc.*)
166                         title="Class utilization for "
167                         vtitle="bandwidth in kilobits/s"
168                         ;;
169
170         esac
171
172         eval "NETDATA_TITLE_${y}=\${NETDATA_TITLE_${y}:-${x}}"
173         eval "t=\${NETDATA_TITLE_${y}}"
174         
175         eval "p=\${NETDATA_PRIORITY_${y}}"
176         if [ ! "$p" = "IGNORE" ]
177         then
178                 cat >>${data}/index.html <<EOF2
179                 addChart('${x}', '${y}_div', 0, 0, "data/${x}/${NETDATA_CONFIG_HISTORY_POINTS}/${NETDATA_CONFIG_UPDATE_EVERY}/average/", "${title}${t} (${x})", "${vtitle}");
180 EOF2
181         fi
182 done
183
184 cat >>${data}/index.html <<EOF3
185                 refreshCharts(999999);
186         }
187         
188         var refreshCount = 0;
189         function myChartsRefresh() {
190                 refreshCount += 2;
191                 if(refreshCount > $NETDATA_CONFIG_RELOAD_EVERY) location.reload();
192
193                 // refresh up to 2 charts per second
194                 refreshCharts(2);
195         }
196         
197         setInterval(myChartsRefresh, 1000);
198
199         //window.onresize = function(event) {
200         //      refreshCharts(999999);
201         //};
202         </script>
203
204         </head>
205
206 <body>
207         <!--
208                 EDIT: add one div per interface you have
209                 EDIT: use the same id above and bellow!
210         -->
211 EOF3
212
213 tmp="/tmp/$RANDOM.netdata.$$.$RANDOM"
214 for x in $all
215 do
216         y=`echo "$x" | tr ".-" "__"`
217         eval "NETDATA_PRIORITY_${y}=\${NETDATA_PRIORITY_${y}:-${x}}"
218         eval "t=\${NETDATA_PRIORITY_${y}}"
219         if [ ! "$t" = "IGNORE" ]
220         then
221                 echo "${t}|${y}"
222         fi
223 done >"${tmp}"
224
225 for x in `cat "${tmp}" | sort`
226 do
227         n="`echo "$x" | cut -d '|' -f 2-`"
228         cat >>${data}/index.html <<EOF4
229         <div id="${n}_div"></div>
230 EOF4
231 done
232 rm -f "${tmp}"
233
234 cat >>${data}/index.html <<EOF5
235  </body>
236 </html>
237 EOF5
238
239 set|grep ^NETDATA_ >netdata.conf
240
241 if [ ! -h "${data}/data" ]
242 then
243         cd "${data}"
244         ln -s . data
245 fi
246
247 echo "All Done."
248 echo "Just hit http://127.0.0.1:19999/ from your browser."
249