File: split-config.pl

package info (click to toggle)
remstats 1.00a4-8woody1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,576 kB
  • ctags: 1,020
  • sloc: perl: 11,706; ansic: 2,776; makefile: 944; sh: 869
file content (225 lines) | stat: -rwxr-xr-x 5,865 bytes parent folder | download
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
#!@@PERL@@ @@PERLOPTS@@

# Copyright 1999, 2000, 2001 (c) Thomas Erskine <@@AUTHOR@@>
# See the COPYRIGHT file with the distribution.

# split-config - convert the old config-file format to
#		the new config-dir form
# $Id: split-config.pl,v 1.4 2001/08/28 15:22:24 remstats Exp $

# - - -   Configuration   - - -

use strict;

# What is this program called, for error-messages and file-names
$main::prog = 'split-config';
# Which files need to be created, empty if necessary?
@main::files = ('alerts', 'archives', 'colors', 'general', 'groups', 
	'html', 'links', 'oids', 'remotepings', 'times', 'tools');
# Which directories need to get created?
@main::dirs = ('customgraphs', 'datapages', 'views', 'view-templates');

# - - -   Version History   - - -

(undef, $main::version) = split(' ', '$Revision: 1.4 $');

# - - -   Setup   - - -

use Getopt::Std;
use lib '.', '@@LIBDIR@@';
require "remstats.pl";

# Parse the command-line
# STRICT use vars qw( $opt_d $opt_h );
getopts('d:h');

if (defined $main::opt_h) { &usage; } # no return
if (defined $main::opt_d) { $main::debug = $main::opt_d; } else { $main::debug = 0; }

unless ($#ARGV == 1) { &usage; } # no return

my $config_file = shift @ARGV;
my $config_dir = shift @ARGV;

unless (-f $config_file) {
	&abort("no such configfile as $config_file");
}

if (-e $config_dir) {
	&abort("configdir $config_dir exists");
}
else {
	mkdir $config_dir, 0755 or
		&abort("can't mkdir $config_dir: $!");
}

# - - -   Mainline   - - -

open (OLD, "<$config_file") or &abort("can't open $config_file: $!");

my %done_files = ();
my %done_dir = ();
my $opened_new = 0;
my $saved_html = '';
my $saved_comments = '';
my ($section_type, $section_name, $section, $filename);
while (<OLD>) {
	chomp;
	&debug("old: $_") if ($main::debug>1);
	if (/^\[(\S+)(\s+(\S+))?\]\s*$/) {
		$section_type = $1;
		$section_name = $3;
		if ($section_type eq 'host' or $section_type eq 'script' or
				$section_type eq 'rrd' or $section_type eq 'remoteping' or
				$section_type eq 'customgraph') {
			$section_type .= 's';
		}
		$done_files{$section_type} = 1;
		if (defined $section_name and $section_name =~ /(.*)-\*$/) {
			$section_name = $1 .'-';
		}
		$section = $section_type . ((defined $section_name) ? ' '. $section_name : '');;
		&debug("starting $section") if ($main::debug);

		if (defined $section_name) {
			unless (-d ($config_dir .'/'. $section_type)) {
				&debug("mkdir for $config_dir/$section_type") if ($main::debug);
				mkdir $config_dir .'/'. $section_type, 0755 or
					&abort("can't mkdir $config_dir/$section_type: $!");
				$done_dir{$section_name} = 1;
			}
			$filename = $config_dir .'/'. $section_type .'/'. $section_name;
		}
		else {
			$filename = $config_dir .'/'. $section_type;
		}

		&debug("writing $filename") if ($main::debug);
		open (NEW, ">$filename") or &abort("can't open $filename: $!");
		$opened_new = 1;
	}
	else {
		if (/^#/ or /^\s*$/) {
			$saved_comments .= $_ ."\n";
			next;
		}
		if ($section_type eq 'general') {
			if (/^groups\s+(.*)/) {
				my @groups = &make_array($1);
				$filename = $config_dir . '/groups';
				open (GROUPS, ">$filename") or &abort("can't open $filename: $!");
				print GROUPS $saved_comments . join("\n", @groups) . "\n";
				close (GROUPS);
				$saved_comments = '';
			}
			elsif (/^[a-z]+(status|url)\s+.*/ or /^htmldir\s/ or /^rrdcgi\s/ or 
					/^imagetype\s/ or /^htmlrefresh\s/ or /^thumbnail\s/ or
					/^metadata\s/ or /^webmaster\s/ or /^pagesas\s/ or
					/^alertflag\s/ or /^uptimeflag\s/) {
				$saved_html .= $saved_comments . $_ . "\n";
				$saved_comments = '';
			}
			else {
				print NEW $saved_comments . $_ . "\n" if ($opened_new);
				$saved_comments = '';
			}
		}
		else {
			print NEW $saved_comments . $_ . "\n" if ($opened_new);
			$saved_comments = '';
		}
	}
}
close (NEW);
close (OLD);

# Write an html config-file
$filename = $config_dir . '/html';
open (HTML, ">$filename") or &abort("can't open $filename: $!");
print HTML <<"EOD_HTML";
# html - things relating to web-page generation

# override these to change the labels in the HTML, e.g. to translate
alertreport		Alert Report
comment			Comment
contact			Contact
customindex		Custom Index
description		Description
groupindex		Group Index
hardware		Hardware
hostindex		Host Index
indices			Indices
ipnumber		IP #
lastupdateon	This page last updated on
links			Links
logreport		Log Report
operatingsystem	Operating System
overallindex	Overall Index
pingindex		Ping Index
quickindex		Quick Index
status			Status
tools			Tools
uptime			Uptime

EOD_HTML
if ($saved_html) { print HTML $saved_html; }
close (HTML);


# Now make sure we got all the files we need
foreach (@main::files) {
	if (defined $done_files{$_}) {
		next;
	}
	else {
		$filename = $config_dir .'/'. $_;
		open (FILE, ">$filename") or &abort("can't open $filename: $!");
		print FILE "# $_ - placeholder for empty section\n";
		close (FILE);
	}
}

# Now make sure that certain directories get created
foreach (@main::dirs) {
	if (defined $done_dir{$_}) {
		next;
	}
	else {
		my $dir = $config_dir .'/'. $_;
		mkdir $dir, 0755 or &abort("can't mkdir $dir: $!");
	}
}

exit 0;

#----------------------------------------------------------------- usage ---
sub usage {
	print STDERR <<"EOD_USAGE";
$main::prog version $main::version
usage: $0 [options] configfile configdir
where options are:
	-d	enable debugging output
	-h	show this help
EOD_USAGE
	exit 0;
}

#----------------------------------------------------------------- debug ---
sub debug {
	my ($msg) = @_;

	if ($main::debug) { print STDERR "DEBUG: $msg\n"; }
0;
}

#------------------------------------------------------------------ abort ---
sub abort {
	my ($msg) = @_;
	print STDERR "main::prog: ABORT: $msg\n";
	exit 1;
}

#----------------------------------------------- keep_strict_happy ---
sub keep_strict_happy {
	$main::opt_h = 0;
}