-
Notifications
You must be signed in to change notification settings - Fork 0
/
99-unmenu-utility.awk
274 lines (241 loc) · 8.06 KB
/
99-unmenu-utility.awk
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
BEGIN {
#define ADD_ON_URL utility
#define ADD_ON_MENU
#define ADD_ON_STATUS NO
#define ADD_ON_TYPE awk
#ADD_ON_HTTP_TAGS=NO
#ADD_ON_PAGE_HEADING=NO
#ADD_ON_HTTP_HEADER=YES
#ADD_ON_OPTIONS=-f unmenu.base.lib.awk -f utility.lib.awk
#define ADD_ON_VERSION 1.0 - contributed by bjp999
#define ADD_ON_VERSION 1.5 - Part of myMain 12-1-10 release, contributed by bjp999
#define ADD_ON_VERSION 1.53 - changes for myMain 3-10-11 release, contributed by bjp999 - 5.0b6 support
#define ADD_ON_VERSION 1.54 - changes for myMain 4-10-11 release, contributed by bjp999 - smartctl support
#UNMENU_RELEASE $Revision$ $Date$
# (c) Copyright bjp999, 2009-2011. All rights reserved.
# This program carries no warranty or guarantee of any kind. It is used strictly at the users risk.
CGI_setup()
cmd = GETARG["cmd"]
dev = GETARG["dev"]
disk = GETARG["disk"]
#print "<p> cmd='" cmd "'</p>"
#print "<p> parm1='" parm1 "'</p>"
#print "<p> parm2='" parm2 "'</p>"
#print "<p> parm3='" parm3 "'</p>"
if ( ScriptDirectory == "" ) {
ScriptDirectory = ".";
}
if ( ConfigFile == "" ) {
ConfigFile = "unmenu.conf";
}
# Local config file variables will never be overwritten by a distributd file.
# It is here where local changes should be made without worry about them being
# lost when a new unmenu.conf file is distributed.
if ( LocalConfigFile == "" ) {
LocalConfigFile = "unmenu_local.conf";
}
GetConfigValues(ScriptDirectory "/" ConfigFile);
GetConfigValues(ScriptDirectory "/" LocalConfigFile);
amp="AMP3RS4ND"
nbsp=amp "nbsp;"
vv[0, "from"] = "what"
vv[1, "from"] = "stuff"
vv[2, "from"] = "ImageURL"
if(GETARG["ImageURL"] == "")
vv[2, "to"] = "http://" MyHost "/log/images";
else
vv[2, "to"] = GETARG["ImageURL"]
vv[3, "from"] = "legend"
vv[3, "to"] = ""
if(cmd == "smartctl") {
if(GETARG["file"] == "")
if(GETARG["smart"] == "")
RunCommand(sprintf("smartctl -a -d ata /dev/%s", dev), disk);
else {
smart=GETARG["smart"]
gsub("%20", " ", smart);
if(index(smart, "/dev") <= 0)
smart = smart " /dev/" dev;
#perr(smart);
RunCommand("smartctl -a " smart, disk);
}
else
RunCommand("fromdos<" GETARG["file"], GETARG["file"], "smartctl " GETARG["file"]);
}
else if (cmd == "hdparm") {
RunCommand(sprintf("hdparm -I /dev/%s", dev), disk);
}
else if (cmd == "samba") {
RunCommand("testparm -s", "", "Samba Configuration");
}
else if (cmd == "test") {
vv[0, "to"] = "RunTest";
vv[1, "to"] = RunTest();
}
else if(cmd == "spinup") {
disk_blocks = GetRawDiskBlocks( "/dev/" dev )
skip_blocks = 1 + int( rand() * disk_blocks );
cmd="dd if=/dev/" dev " of=/dev/null count=1 bs=1k skip=" skip_blocks " >/dev/null 2>&1"
system(cmd);
vv[0, "to"] = "Spinup " dev;
vv[0, "to"] = "Done"
}
else if(cmd == "syslog") {
SyslogHtml(GETARG["syslog"], GETARG["style"], (1==0));
}
else if (cmd == "spindown") {
cmd="/usr/sbin/hdparm -y /dev/" dev " >/dev/null 2>&1"
system(cmd);
vv[0, "to"] = "Spindown " dev;
vv[1, "to"] = "Done"
}
else if (cmd == "syslogx") {
syslog=GETARG["syslog"]
if (syslog=="") {
syslog="/var/log/syslog"
}
#vv[0, "to"] = "Filtered syslog (disk=\"" disk ", dev=\"" dev ")"
FilterSyslogx(syslog,dev,disk)
}
else if (cmd == "syslogRobJ") {
syslog=GETARG["syslog"]
if (syslog=="") {
syslog="/var/log/syslog"
}
#fn="/tmp/temp"
vv[0, "to"] = "Filtered syslog (disk='" disk "' , device='" dev "')"
FilterSyslogRobJ(syslog,dev,disk) > fn
#close(fn);
#vv[1, "to"] = GetSysLog(0, "/tmp/temp");
}
else if (cmd == "smarthistory") {
SMART_HISTORY_DIR = CONFIG["SMART_HISTORY_DIR"] ? CONFIG["SMART_HISTORY_DIR"] : "/boot/smarthistory"
SMART_HISTORY_CMD = CONFIG["SMART_HISTORY_CMD"] ? CONFIG["SMART_HISTORY_CMD"] : "./smarthistory -wake ON -output HTML -graph IMAGE -report ALL -devices /dev/" dev
RunCommand("cd " SMART_HISTORY_DIR "; " SMART_HISTORY_CMD);
}
fn="UtilityShell.html"
htmlLoadFile(fn, html)
#for(i=0; i<html["count"]; i++) {
# perr(i " " html[i, "type"] " --> " html[i]);
#}
if(OptionToLoadSyslog)
htmlExpandGroup(html, "A");
htmlExpandGroup(html, "H"); #include html "head" section
ss = htmlSerialize(html, 0, vv);
#perr(ss);
gsub(amp, "\\&", ss);
print ss;
}
function RunCommand(cmd, disk, output) {
RS="\n"
if(output == "")
vv[0, "to"] = cmd " (" disk ")"
else
vv[0, "to"] = output
output = ""
while (( cmd | getline f ) > 0)
output = output f "<br>"
vv[1, "to"] = output
#perr(output);
close(cmd)
}
function FilterSyslogRobJ(syslog, dev, disk, cmd, f)
{
if ( substr(dev,1,2) == "hd" ) { # IDE devices
if ( disk == "parity" )
cmd = "|md0: [^p]"
else
cmd = "|md" substr(disk, 5) "[^0-9]"
}
else { # SATA or other SCSI devices
#Step 1 - Find the ata#s
cmd = "cat " syslog "|grep '\\[" dev "\\]'"
#p(cmd)
cmd|getline f
close(cmd)
split(f,d);
t=d[7];
delete d;
split(t, d, ":")
atanum=d[1]
atanum2=d[3]
#p(dev ", " disk ", " atanum ", " atanum2);
if ( disk == "flash" )
cmd = "|scsi" atanum " :|scsi " atanum ":0:" atanum2 ":0"
else if ( disk == "parity" )
cmd = "|scsi" atanum " :|scsi " atanum ":0:" atanum2 ":0| ata" atanum ":| ata" atanum ".0" atanum2 ":|md0: [^p]"
else
cmd = "|scsi" atanum " :|scsi " atanum ":0:" atanum2 ":0| ata" atanum ":| ata" atanum ".0" atanum2 ":|" disk "[^0-9]| md" substr(disk, 5) ":"
}
cmd = "cat " syslog "|egrep \"(" dev cmd ")\""
#p(cmd)
first=(1==1)
fn="/tmp/temp"
while (( cmd | getline f ) > 0)
if(first)
print f>fn
else
print f>>fn
#perr(output);
close(cmd)
close(fn)
t = GETARG["style"]
if(length(t) > 1)
t = substr(t, 2, 1)
vv[1, "to"] = GetSysLog(0, "/tmp/temp", t+0);
}
function FilterSyslogx(syslog, dev, disk)
{
#Step 1 - Find the ata#
cmd = "cat " syslog "|grep '\\[" dev "\\]'"
#p(cmd)
cmd|getline f
close(cmd)
split(f,d);
t=d[7];
delete d;
split(t, d, ":")
atanum=d[1]
#p(dev ", " disk ", " atanum);
cmd = "cat " syslog "|egrep \"(" dev "|" disk "[^0-9]|md" substr(disk, 5) "[^0-9]|ata[ ]*" atanum "[^0-9])\"";
#p(cmd)
RunCommand(cmd, "Filtered syslog for "disk)
}
function RunTest()
{
t="abcdefg1hij2kmmm3lmnop1hij2kmmm3asdfasdf1hij2kmmm3asdfsadf1hij2kmmm3"
t1=t
gsub("1.*2", "", t1)
gsub("3", "", t1);
return t "<br>" t1 "<br>"
}
#-------------------------------------------
# Utility function to perform debug prints.
#-------------------------------------------
#function p(printme)
#{
# gsub("<", "\\<", printme);
# gsub(">", "\\>", printme);
# print "<p>"printme"</p>"
#}
# open and read the unmenu configuration file. In it, look for lines with the following pattern:
#variableName = ReplacementValue
# The values found there can be used to override values of some variables in these scripts.
# the CONFIG[] array is set with the variable.
function GetConfigValues(cfile) {
RS="\n"
while (( getline line < cfile ) > 0 ) {
delete c;
match( line , /^([^# \t=]+)([\t ]*)(=)([\t ]*)(.+)/, c)
#print c[1,"length"] " " c[2,"length"] " " c[3,"length"] " " c[4, "length"] " " c[5, "length"] " " line
if ( c[1,"length"] > 0 && c[2,"length"] > 0 &&
c[3,"length"] > 0 && c[4, "length"] > 0 && c[5, "length"] > 0 ) {
CONFIG[substr(line,c[1,"start"],c[1,"length"])] = substr(line,c[5,"start"],c[5,"length"])
if ( DebugMode == "yes" ) {
print "importing from unmenu.conf: " \
"CONFIG[" substr(line,c[1,"start"],c[1,"length"]) "] = " substr(line,c[5,"start"],c[5,"length"])
}
}
}
close(cfile);
}