-
Notifications
You must be signed in to change notification settings - Fork 0
/
xymnote_editor_4.3.pl
289 lines (265 loc) · 7.47 KB
/
xymnote_editor_4.3.pl
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/perl
#
#############################################
# Name: xymnote_editor.cgi
# Xymon update: Galen Johnson
# Original Author: Chris Naude
# Purpose: Manage HTML notes files
# Version: 1.0
# The text2html module can be downloaded from CPAN #############################################
use strict;
use CGI ":standard";
use HTML::FromText;
# Your $XYMONHOME
my $xymon_home = "/usr/share/xymon";
my $xymon_cmd = "$xymon_home/bin/xymon";
# xymon config file
my $xymoncfg = "$xymon_home/etc/xymonserver.cfg";
# The actual www notes dir on the server
my $xymon_notes = "/var/www/xymon/notes"; # This is where the data files are stored.
# Not to be confused with the default notes dir.
my $xymon_notesdata = "$xymon_home/etc/notesdata";
my $xymon_header = "$xymon_home/web/notes_header"; # Change this if needed.
my $xymon_footer = "$xymon_home/web/notes_footer"; # Change this if needed.
#Standard hosts.cfg file
my $xymon_hosts = "$xymon_home/hosts.cfg";
my $xymon_menu = "$xymon_home/etc/xymonmenu.cfg";
# No changes needed below.
my $version = "4.3.28";
my $color = "blue";
my %hosts;
my $cmd = param("cmd");
my $host = param("host");
$host =~ s/(.*?)\///g;
$host =~ s/(\.html)$//g;
my $note = param("note");
my @lines;
# Set up the environment variables and dynamic variables from xymonserver.cfg
foreach (`$xymon_home/bin/xymoncmd env | /usr/bin/grep XYMON`) {
chomp;
next if $_ =~ /^directory/;
my ($var,$val) = /^\s*(.*?)\s*=\s*(.*)/;
$ENV{$var} = $val;
}
sub print_notesdata {
open (NOTESDATA, "<$xymon_notesdata/${host}") or &print_error("I can't read from $xymon_notesdata/$host!");
while (<NOTESDATA>) {
print;
}
close NOTESDATA;
}
sub write_notesdata {
open (NOTESDATA, ">$xymon_notesdata/$host") or &print_error("I can't write to $xymon_notesdata/$host!");
print NOTESDATA $note;
close NOTESDATA;
}
sub make_note {
my ($color) = @_;
my $note = "$xymon_notes/${host}.html";
open (NOTE, ">$note") or &print_error("I can't open $note for writing!");
select NOTE;
&print_header($color);
&print_notesdata;
&print_footer($color);
select STDOUT;
close NOTE;
}
sub save_note {
&write_notesdata;
&make_note('blue');
&get_note;
print '<center><b><font color="white">Note saved.</font></b></center><p>';
&print_note;
}
sub edit_note {
if ($cmd =~ /add html/) {
my $t2h = HTML::FromText->new({
blockcode => 1,
lines => 1,
tables => 1,
bullets => 1,
numbers => 1,
urls => 1,
email => 1,
bold => 1,
underline => 1,
});
$note = $t2h->parse( $note );
#$note =~ s/\n/<br>/sgi; #
}
if ($cmd =~ /strip html/) {
$note =~ s/<.*?>//sgi;
}
print <<HTML;
<CENTER>
<TABLE BORDER="1" CELLPADDING="3">
<CAPTION>
<H2><CENTER>$host [$hosts{$host}]</CENTER></H2>
</CAPTION>
<TR>
<TD ALIGN="CENTER">
<form method="POST">
<input type="hidden" name="host" value="$host">
<TEXTAREA ROWS="35" COLS="80" NAME="note" STYLE="background-color:#000033;color:dddddd">
HTML
if ($note) {
print $note;
} elsif ($lines[0]) {
print @lines;
} else {
print '<!-- Remember to use proper HTML formatting here. -->'; }
print <<HTML;
</TEXTAREA><br>
<input name="cmd" value="preview" type="submit"> <input name="cmd" value="add html tags" type="submit"> <input name="cmd" value="strip html tags" type="submit"> <input name="cmd" value="cancel" type="submit">
</form>
</TD>
</TR>
</TABLE>
</CENTER>
HTML
}
sub print_note {
print <<HTML;
<CENTER>
<TABLE WIDTH="75%" BORDER="1" CELLPADDING="3">
<CAPTION>
<CENTER><H2>$host [$hosts{$host}]</H2></CENTER>
</CAPTION>
<TR>
<TD>
HTML
if ($lines[0]) {
print @lines;
print <<HTML;
</TD>
</TR>
<TR>
<TD ALIGN="CENTER">
<form method="POST">
<input type="hidden" name="host" value="$host">
HTML
} elsif ($cmd =~ /preview/) {
print <<HTML;
$note
</TD>
</TR>
<TR>
<TD ALIGN="CENTER">
<form method="POST">
<input type="hidden" name="host" value="$host"> <input name="cmd" value="save" type="submit">
HTML
} else {
print <<HTML;
The are no notes for $host [$hosts{$host}].
</TD>
</TR>
<TR>
<TD ALIGN="CENTER">
<form method="POST">
<input type="hidden" name="host" value="$host">
HTML
}
print <<HTML;
<input type="hidden" name="note" value='$note'> <input name="cmd" value="edit" type="submit"> <input name="cmd" value="list" type="submit">
</form>
</TD>
</TR>
</TABLE>
</CENTER>
HTML
}
#
sub print_error {
my $error = shift;
print "<center><b><font color=\"red\">$error</font></b></center><p>";
}
sub get_note {
if ( -s "$xymon_notesdata/$host") {
open (NOTE, "<$xymon_notesdata/$host") or &print_error("I can't open $xymon_notesdata/$host for reading!");
while (my $note = <NOTE>) {
push @lines, $note;
}
close NOTE;
}
}
sub print_menu {
print '<CENTER><TABLE BORDER="1" CELLPADDING="3"><CAPTION><H2><CENTER>Xymon Notes</CENTER></H2><b></b></CAPTION>';
for my $host(sort keys %hosts) {
print <<HTML;
<TR><TD>$host</TD><TD>$hosts{$host}</TD>
<TD><form method="POST"><input type="hidden" name="host" value="$host"> <input name="cmd" value="view" type="submit"> <input name="cmd" value="edit" type="submit"></form></TD></TR>
HTML
}
print '</TABLE></CENTER>';
}
sub get_hosts {
open (HOSTS, "-|", $xymon_cmd, '127.0.0.1', 'hostinfo') or &print_error("I can't open $xymon_hosts!");
while (<HOSTS>) {
my ($hostname, $hostip, @dummy) = split('\|');
$hosts{$hostname} = $hostip;
}
close HOSTS;
}
sub print_header {
my $color = shift;
print "Content-type: text/html; charset=iso-8859-1\n\n";
open (HEAD, "<$xymon_header") or &print_error("I can't open $xymon_header for reading!");
while (<HEAD>) {
# It's a bit hard to edit with a refresh ;)
if (/META/i && /HTTP-EQUIV/i && /REFRESH/i && /CONTENT/i) { s/<(.*?)>/<!-- Refresh removed -->/g; }
s/&XYMONLOGO/$ENV{'XYMONLOGO'}/g;
s/&XYMONBACKGROUND/$color/g;
s/&XYMONBODYCSS/$ENV{'XYMONBODYCSS'}/g;
s/&XYMONBODYMENUCSS/$ENV{'XYMONBODYMENUCSS'}/g;
s/&XYMONMENUSKIN/$ENV{'XYMONMENUSKIN'}/g;
s/&XYMONPAGEPATH/$ENV{'XYMONPAGEPATH'}/g;
s/&XYMONSKIN/$ENV{'XYMONSKIN'}/g;
s/&XYMONDATE/$ENV{'XYMONDATE'}/g;
if (/&XYMONBODYHEADER/) {
s/&XYMONBODYHEADER//g;
open (MENU, "<$xymon_menu") or &print_error("I can't open $xymon_menu for reading!");
while (<MENU>) {
s/\$XYMONSERVERWWWURL/$ENV{'XYMONSERVERWWWURL'}/g;
s/\$XYMONSERVERCGIURL/$ENV{'XYMONSERVERCGIURL'}/g;
s/\$XYMONSERVERSECURECGIURL/$ENV{'XYMONSERVERSECURECGIURL'}/g;
print;
}
close MENU;
}
print;
}
close HEAD;
}
sub print_footer {
open (FOOT, "<$xymon_footer") or &print_error("I can't open $xymon_footer for reading!");
while (<FOOT>) {
s/&XYMONMENUSKIN/$ENV{'XYMONMENUSKIN'}/g;
s/&XYMONDREL/$version/g;
print;
}
close FOOT;
}
# Main
my ($oldbar) = $|;
my $cfh = select (STDOUT);
$| = 1;
&get_hosts;
&print_header($color); # I like blue ;)
if ($cmd =~ /edit|html/) {
&get_note;
&edit_note;
} elsif ($cmd eq 'view') {
&get_note;
&print_note;
} elsif ($cmd eq 'preview') {
&print_note;
} elsif ($cmd eq 'preview as html') {
&print_note;
} elsif ($cmd eq 'save') {
&save_note;
} else {
&print_menu;
}
&print_footer;
$| = $oldbar;
select ($cfh);