tazpanel view index.cgi @ rev 191

Add a small terminal emulator (wih wget support to upload file on remote machine)
author Christophe Lincoln <pankso@slitaz.org>
date Fri Jun 10 21:40:07 2011 +0200 (2011-06-10)
parents dbb91ac456b7
children c05f8958b18b
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='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 *)
132 [ "$cmd" == "" ] || \
133 gettext "Unknow command: $cmd"
134 gettext "Commands:"; echo " $commands" ;;
135 esac
136 echo '</pre>' ;;
137 *\ top\ *)
138 TITLE="- $(gettext "Process activity")"
139 xhtml_header
140 echo '<pre>'
141 top -n1 -b | sed \
142 -e s"#^[A-Z].*:\([^']\)#<span class='sh-comment'>\0</span>#"g \
143 -e s"#PID.*\([^']\)#<span class='top'>\0</span>#"g
144 echo '</pre>' ;;
145 *\ debug\ *)
146 TITLE="- Debug"
147 xhtml_header
148 echo '<h2>HTTP Environment</h2>'
149 echo '<pre>'
150 httpinfo
151 echo '</pre>' ;;
152 *\ report\ *)
153 TITLE="- $(gettext "System report")"
154 [ -d /var/cache/slitaz ] || mkdir -p /var/cache/slitaz
155 output=/var/cache/slitaz/sys-report.html
156 xhtml_header
157 echo "<h2>$(gettext "Reporting to:") $output</h2>"
158 echo '<pre>'
159 gettext "Creating report header... "
160 cat > $output << EOT
161 <!DOCTYPE html>
162 <html xmlns="http://www.w3.org/1999/xhtml">
163 <head>
164 <title>SliTaz system report</title>
165 <style type="text/css">
166 body { padding: 20px 60px; font-size: 13px; } h1, h2 { color: #444; }
167 pre { background: #f1f1f1; border: 1px solid #ddd;
168 padding: 10px; border-radius: 4px; }
169 span.diff-rm { color: red; }
170 span.diff-add { color: green; }
171 </style>
172 </head>
173 <body>
174 EOT
175 ok_status
176 gettext "Creating system summary... "
177 cat >> $output << EOT
178 <h1>SliTaz system report</h1>
179 Date: $(date)
180 <pre>
181 uptime : $(uptime)
182 cmdline : $(cat /proc/cmdline)
183 version : $(cat /etc/slitaz-release)
184 packages : $(ls /var/lib/tazpkg/installed | wc -l) installed
185 kernel : $(uname -r)
186 </pre>
187 EOT
188 ok_status
189 gettext "Getting hardware info... "
190 cat >> $output << EOT
191 <h2>free</h2>
192 <pre>
193 $(free)
194 </pre>
196 <h2>lspci -k</h2>
197 <pre>
198 $(lspci -k)
199 </pre>
201 <h2>lsusb</h2>
202 <pre>
203 $(lsusb)
204 </pre>
206 <h2>lsmod</h2>
207 <pre>
208 $(lsmod)
209 </pre>
211 EOT
212 ok_status
213 gettext "Getting networking info... "
214 cat >> $output << EOT
215 <h2>ifconfig -a</h2>
216 <pre>
217 $(ifconfig -a)
218 </pre>
219 <h2>route -n</h2>
220 <pre>
221 $(route -n)
222 </pre>
223 <h2>/etc/resolv.conf</h2>
224 <pre>
225 $(cat /etc/resolv.conf)
226 </pre>
227 EOT
228 ok_status
229 gettext "Getting filesystems info..."
230 cat >> $output << EOT
231 <h2>blkid</h2>
232 <pre>
233 $(blkid)
234 </pre>
235 <h2>fdisk -l</h2>
236 <pre>
237 $(fdisk -l)
238 </pre>
239 <h2>mount</h2>
240 <pre>
241 $(mount)
242 </pre>
243 <h2>df -h</h2>
244 <pre>
245 $(df -h)
246 </pre>
247 <h2>df -i</h2>
248 <pre>
249 $(df -i)
250 </pre>
251 EOT
252 ok_status
253 gettext "Getting boot logs... "
254 cat >> $output << EOT
255 <h2>$(gettext "Kernel messages")</h2>
256 <pre>
257 $(cat /var/log/dmesg.log)
258 </pre>
259 <h2>$(gettext "Boot scripts")</h2>
260 <pre>
261 $(cat /var/log/boot.log | filter_taztools_msgs)
262 </pre>
263 EOT
264 ok_status
265 gettext "Creating report footer... "
266 cat cat >> $output << EOT
267 </body>
268 </html>
269 EOT
270 ok_status
271 echo '</pre>'
272 echo "<p><a class='button' href='$SCRIPT_NAME?file=$output'>
273 $(gettext "View report")</a>"
274 gettext "This report can be attached with a bug report on: "
275 echo '<a href="http://bugs.slitaz.org/">bugs.slitaz.org</a></p>' ;;
276 *)
277 #
278 # Default xHTML content
279 #
280 xhtml_header
281 [ -n "$(GET gen_locale)" ] && new_locale=$(GET gen_locale)
282 [ -n "$(GET rdate)" ] && echo ""
283 cat << EOT
284 <div id="wrapper">
285 <h2>$(gettext "Host:") $(hostname)</h2>
286 <p>$(gettext "SliTaz administration and configuration Panel")<p>
287 </div>
288 <div id="actions">
289 <a class="button" href="$SCRIPT_NAME?terminal">
290 <img src="$IMAGES/terminal.png" />$(gettext "Terminal")</a>
291 <a class="button" href="$SCRIPT_NAME?top">
292 <img src="$IMAGES/monitor.png" />$(gettext "Process activity")</a>
293 <a class="button" href="$SCRIPT_NAME?report">
294 <img src="$IMAGES/text.png" />$(gettext "Create a report")</a>
295 </div>
297 <h3>$(gettext "Summary")</h3>
298 <div id="summary">
299 <pre>
300 $(gettext "Uptime :")$(uptime)
301 $(gettext "Memory in Mb :") $(free -m | grep Mem: | awk \
302 '{print "Total:", $2, "Used:", $3, "Free:", $4}')
303 $(gettext "Linux kernel :") $(uname -r)
304 </pre>
305 <!-- Close summary -->
306 </div>
308 <h4>$(gettext "Network status")</h4>
309 $(list_network_interfaces)
311 <h4>$(gettext "Filesystem usage statistics")</h4>
312 EOT
313 # Disk stats (management is done as hardware.cgi)
314 table_start
315 df_thead
316 df -h | grep ^/dev | while read fs size used av pct mp
317 do
318 cat << EOT
319 <tr>
320 <td><a href="hardware.cgi">
321 <img src="$IMAGES/harddisk.png" />${fs#/dev/}</a></td>
322 <td>$size</td>
323 <td>$av</td>
324 <td class="pct"><div class="pct"
325 style="width: $pct;">$used - $pct</div></td>
326 <td>$mp</td>
327 </tr>
328 EOT
329 done
330 table_end
331 cat << EOT
332 <h3>$(gettext "Panel Activity")</h3>
333 <pre id="panel-activity">
334 $(cat $LOG_FILE | tail -n 8 | sort -r | syntax_highlighter activity)
335 </pre>
337 EOT
338 ;;
339 esac
341 xhtml_footer
342 exit 0