tazpanel view index.cgi @ rev 194

Add cat support in terminal and fix log file path
author Christophe Lincoln <pankso@slitaz.org>
date Fri Jun 10 22:29:16 2011 +0200 (2011-06-10)
parents d65aa88d638f
children 4a4d48fb6190
line source
1 #!/bin/sh
2 #
3 # Main CGI interface for TazPanel. In on word: KISS. Use the main css form
4 # command so we are faster and do not load unneeded functions. If necessary
5 # you can use the lib/ dir to handle external resources.
6 #
7 # Copyright (C) 2011 SliTaz GNU/Linux - BSD License
8 #
10 # Common functions from libtazpanel
11 . lib/libtazpanel
12 get_config
13 header
15 # Check whether a configuration file has been modified after installation
16 file_is_modified()
17 {
18 grep -l " $1$" $INSTALLED/*/md5sum | while read file; do
20 # Found, but can we do diff ?
21 [ "$(grep -h " $1$" $file)" != "$(md5sum $1)" ] || break
22 org=$(dirname $file)/volatile.cpio.gz
23 zcat $org 2>/dev/null | cpio -t 2>/dev/null | \
24 grep -q "^${1#/}$" || break
26 case "$2" in
27 diff)
28 tmp=/tmp/tazpanel$$
29 mkdir -p $tmp
30 ( cd $tmp ; zcat $org | cpio -id ${1#/} )
31 diff -u $tmp$1 $1
32 rm -rf $tmp ;;
33 button)
34 cat <<EOT
35 <a class="button" href='$SCRIPT_NAME?file=$1&action=diff'>
36 <img src="$IMAGES/help.png" />`gettext "Differences"`</a>
37 EOT
38 esac
39 break
40 done
41 }
43 #
44 # Things to do before displaying the page
45 #
47 [ -n "$(GET panel_pass)" ] &&
48 sed -i s@/:root:.*@/:root:$(GET panel_pass)@ $HTTPD_CONF
50 #
51 # Commands
52 #
54 case " $(GET) " in
55 *\ file\ *)
56 #
57 # Handle files
58 #
59 file="$(GET file)"
60 case $file in
61 *.html)
62 cat $file && exit 0 ;;
63 *)
64 TITLE="- File"
65 xhtml_header
66 echo "<h2>$file</h2>" ;;
67 esac
68 if [ "$(GET action)" == "edit" ]; then
69 cat <<EOT
70 <form method="post" action="$SCRIPT_NAME?file=$file">
71 <img src="$IMAGES/edit.png" />
72 <input type="submit" value="`gettext "Save"`">
73 <textarea name="content" rows="30" style="width: 100%;">
74 $(cat $file)
75 </textarea>
76 </form>
77 EOT
78 elif [ "$(GET action)" == "diff" ]; then
79 echo '<pre id="diff">'
80 file_is_modified $file diff | syntax_highlighter diff
81 echo '</pre>'
82 else
83 [ -n "$(POST content)" ] &&
84 sed "s/`echo -en '\r'` /\n/g" > $file <<EOT
85 $(POST content)
86 EOT
87 cat <<EOT
88 <div id="actions">
89 <a class="button" href='$SCRIPT_NAME?file=$file&action=edit'>
90 <img src="$IMAGES/edit.png" />`gettext "Edit"`</a>
91 EOT
92 file_is_modified $file button
93 echo -e "</div>\n<pre>"
94 # Handle file type by extension as a Web Server does it.
95 case "$file" in
96 *.conf|*.lst)
97 syntax_highlighter conf ;;
98 *.sh|*.cgi)
99 syntax_highlighter sh ;;
100 *)
101 cat ;;
102 esac < $file
103 echo '</pre>'
104 fi ;;
105 *\ terminal\ *|*\ cmd\ *)
106 # Cmdline terminal.
107 commands='cat du help ls ping pwd who wget'
108 cmd=$(GET cmd)
109 TITLE="- $(gettext "Terminal")"
110 xhtml_header
111 cat << EOT
112 <form method="get" action="$SCRIPT_NAME">
113 <div class="box">
114 root@$(hostname):~# <input type="text" name="cmd" style="width: 80%;" />
115 </div>
116 </form>
117 EOT
118 echo '<pre id="terminal">'
119 # Allow only a few commands for the moment.
120 case "$cmd" in
121 usage|help)
122 gettext -e "Small terminal emulator, commands options are supported.\n"
123 gettext "Commands:"; echo " $commands" ;;
124 wget*)
125 dl=/var/cache/downloads
126 [ ! -d "$dl" ] && mkdir -p $dl
127 gettext "Downloading to:"; echo " $dl"
128 cd $dl && $cmd ;;
129 du*|ls*|ping*|pwd|who)
130 $cmd ;;
131 cat*)
132 # Cmd must be used with an arg.
133 arg=$(echo $cmd | awk '{print $2}')
134 [ "$arg" == "" ] && echo -n "$cmd " && \
135 gettext "needs an arument $arg" && exit 0
136 $cmd ;;
137 *)
138 [ "$cmd" == "" ] || \
139 gettext "Unknow command: $cmd"
140 gettext "Commands:"; echo " $commands" ;;
141 esac
142 echo '</pre>' ;;
143 *\ top\ *)
144 TITLE="- $(gettext "Process activity")"
145 xhtml_header
146 echo '<pre>'
147 top -n1 -b | sed \
148 -e s"#^[A-Z].*:\([^']\)#<span class='sh-comment'>\0</span>#"g \
149 -e s"#PID.*\([^']\)#<span class='top'>\0</span>#"g
150 echo '</pre>' ;;
151 *\ debug\ *)
152 TITLE="- Debug"
153 xhtml_header
154 echo '<h2>HTTP Environment</h2>'
155 echo '<pre>'
156 httpinfo
157 echo '</pre>' ;;
158 *\ report\ *)
159 TITLE="- $(gettext "System report")"
160 [ -d /var/cache/slitaz ] || mkdir -p /var/cache/slitaz
161 output=/var/cache/slitaz/sys-report.html
162 xhtml_header
163 echo "<h2>$(gettext "Reporting to:") $output</h2>"
164 echo '<pre>'
165 gettext "Creating report header... "
166 cat > $output << EOT
167 <!DOCTYPE html>
168 <html xmlns="http://www.w3.org/1999/xhtml">
169 <head>
170 <title>SliTaz system report</title>
171 <style type="text/css">
172 body { padding: 20px 60px; font-size: 13px; } h1, h2 { color: #444; }
173 pre { background: #f1f1f1; border: 1px solid #ddd;
174 padding: 10px; border-radius: 4px; }
175 span.diff-rm { color: red; }
176 span.diff-add { color: green; }
177 </style>
178 </head>
179 <body>
180 EOT
181 ok_status
182 gettext "Creating system summary... "
183 cat >> $output << EOT
184 <h1>SliTaz system report</h1>
185 Date: $(date)
186 <pre>
187 uptime : $(uptime)
188 cmdline : $(cat /proc/cmdline)
189 version : $(cat /etc/slitaz-release)
190 packages : $(ls /var/lib/tazpkg/installed | wc -l) installed
191 kernel : $(uname -r)
192 </pre>
193 EOT
194 ok_status
195 gettext "Getting hardware info... "
196 cat >> $output << EOT
197 <h2>free</h2>
198 <pre>
199 $(free)
200 </pre>
202 <h2>lspci -k</h2>
203 <pre>
204 $(lspci -k)
205 </pre>
207 <h2>lsusb</h2>
208 <pre>
209 $(lsusb)
210 </pre>
212 <h2>lsmod</h2>
213 <pre>
214 $(lsmod)
215 </pre>
217 EOT
218 ok_status
219 gettext "Getting networking info... "
220 cat >> $output << EOT
221 <h2>ifconfig -a</h2>
222 <pre>
223 $(ifconfig -a)
224 </pre>
225 <h2>route -n</h2>
226 <pre>
227 $(route -n)
228 </pre>
229 <h2>/etc/resolv.conf</h2>
230 <pre>
231 $(cat /etc/resolv.conf)
232 </pre>
233 EOT
234 ok_status
235 gettext "Getting filesystems info..."
236 cat >> $output << EOT
237 <h2>blkid</h2>
238 <pre>
239 $(blkid)
240 </pre>
241 <h2>fdisk -l</h2>
242 <pre>
243 $(fdisk -l)
244 </pre>
245 <h2>mount</h2>
246 <pre>
247 $(mount)
248 </pre>
249 <h2>df -h</h2>
250 <pre>
251 $(df -h)
252 </pre>
253 <h2>df -i</h2>
254 <pre>
255 $(df -i)
256 </pre>
257 EOT
258 ok_status
259 gettext "Getting boot logs... "
260 cat >> $output << EOT
261 <h2>$(gettext "Kernel messages")</h2>
262 <pre>
263 $(cat /var/log/dmesg.log)
264 </pre>
265 <h2>$(gettext "Boot scripts")</h2>
266 <pre>
267 $(cat /var/log/boot.log | filter_taztools_msgs)
268 </pre>
269 EOT
270 ok_status
271 gettext "Creating report footer... "
272 cat cat >> $output << EOT
273 </body>
274 </html>
275 EOT
276 ok_status
277 echo '</pre>'
278 echo "<p><a class='button' href='$SCRIPT_NAME?file=$output'>
279 $(gettext "View report")</a>"
280 gettext "This report can be attached with a bug report on: "
281 echo '<a href="http://bugs.slitaz.org/">bugs.slitaz.org</a></p>' ;;
282 *)
283 #
284 # Default xHTML content
285 #
286 xhtml_header
287 [ -n "$(GET gen_locale)" ] && new_locale=$(GET gen_locale)
288 [ -n "$(GET rdate)" ] && echo ""
289 cat << EOT
290 <div id="wrapper">
291 <h2>$(gettext "Host:") $(hostname)</h2>
292 <p>$(gettext "SliTaz administration and configuration Panel")<p>
293 </div>
294 <div id="actions">
295 <a class="button" href="$SCRIPT_NAME?terminal">
296 <img src="$IMAGES/terminal.png" />$(gettext "Terminal")</a>
297 <a class="button" href="$SCRIPT_NAME?top">
298 <img src="$IMAGES/monitor.png" />$(gettext "Process activity")</a>
299 <a class="button" href="$SCRIPT_NAME?report">
300 <img src="$IMAGES/text.png" />$(gettext "Create a report")</a>
301 </div>
303 <h3>$(gettext "Summary")</h3>
304 <div id="summary">
305 <pre>
306 $(gettext "Uptime :")$(uptime)
307 $(gettext "Memory in Mb :") $(free -m | grep Mem: | awk \
308 '{print "Total:", $2, "Used:", $3, "Free:", $4}')
309 $(gettext "Linux kernel :") $(uname -r)
310 </pre>
311 <!-- Close summary -->
312 </div>
314 <h4>$(gettext "Network status")</h4>
315 $(list_network_interfaces)
317 <h4>$(gettext "Filesystem usage statistics")</h4>
318 EOT
319 # Disk stats (management is done as hardware.cgi)
320 table_start
321 df_thead
322 df -h | grep ^/dev | while read fs size used av pct mp
323 do
324 cat << EOT
325 <tr>
326 <td><a href="hardware.cgi">
327 <img src="$IMAGES/harddisk.png" />${fs#/dev/}</a></td>
328 <td>$size</td>
329 <td>$av</td>
330 <td class="pct"><div class="pct"
331 style="width: $pct;">$used - $pct</div></td>
332 <td>$mp</td>
333 </tr>
334 EOT
335 done
336 table_end
337 cat << EOT
338 <h3>$(gettext "Panel Activity")</h3>
339 <pre id="panel-activity">
340 $(cat $LOG_FILE | tail -n 8 | sort -r | syntax_highlighter activity)
341 </pre>
343 EOT
344 ;;
345 esac
347 xhtml_footer
348 exit 0