File: snap

package info (click to toggle)
powerpc-ibm-utils 1.2.12-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,656 kB
  • sloc: ansic: 13,684; sh: 3,088; perl: 832; makefile: 110
file content (345 lines) | stat: -rwxr-xr-x 7,721 bytes parent folder | download | duplicates (3)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/perl -w
# Copyright (c) 2003, 2004 International Business Machines
# Common Public License Version 1.0 (see COPYRIGHT)
#
# Author Todd Inglett <tinglett@us.ibm.com>
# updates by Michael Strosaker <strosake@us.ibm.com>

# Snapshot system config
# Command-line parameters:
#    a:       all data; collect detailed information (more files and output)
#    d dir:   specify the directory where files and output will be collected
#               (default: /tmp/ibmsupt)
#    h:       print this help message
#    o file:  specify the output file (.tar required, .tar.gz optional)
#               (default: snap.tar.gz)
#    v:       verbose output
#
#  Exit codes (view with "echo $?" immediately after running):
#    0:  snap data was successfully captured
#    1:  invalid command line
#    2:  other fatal error

use Getopt::Std;

$outdir = "/tmp/ibmsupt";	# note NO trailing /
$outfile = "snap.tar.gz";	# in the working dir.

#  Files to include in all snaps
@snap_paths_general = (
  "/var/log/messages",
  "/var/log/platform",
  "/var/log/scanoutlog.*",
#  "/proc/bus/pci",	?? binary file
  "/proc/cmdline",
  "/proc/cpuinfo",
#  "/proc/device-tree",  must summarize device-tree (a la dmpdt_chrp) instead of dumping whole thing
  "/proc/devices",
  "/proc/dma",
  "/proc/filesystems",
  "/proc/fs",
  "/proc/ide",
  "/proc/interrupts",
  "/proc/iomem",
  "/proc/ioports",
  "/proc/loadavg",
  "/proc/locks",
  "/proc/mdstat",
  "/proc/meminfo",
  "/proc/misc",
  "/proc/modules",
  "/proc/mounts",
  "/proc/net",
  "/proc/partitions",
  "/proc/pci",
  "/proc/ppc64/lparcfg",
  "/proc/ppc64/eeh",
  "/proc/ppc64/pci",
  "/proc/ppc64/systemcfg",
  "/proc/scsi",
  "/proc/slabinfo",
  "/proc/stat",
  "/proc/swaps",
  "/proc/sys",
  "/proc/sysvipc",
  "/proc/uptime",
  "/proc/version",
  "/dev/nvram",
  "/etc/fstab",
  "/etc/raidtab",
  "/etc/yaboot.conf",
);

#  Files to include in all snaps on SuSE systems
@snap_paths_general_SuSE = (
  "/etc/SuSE-release",
  "/var/log/boot.msg",
);

#  Files to include in all snaps on RedHat systems
@snap_paths_general_RedHat = (
  "/etc/redhat-release",
  "/var/log/dmesg",
);

#  Files to include only in detailed snaps (-a option)
@snap_paths_detailed = (
  "/proc/tty",
  "/etc/inittab",
  "/proc/ppc64/paca",
  "/proc/ppc64/naca",
  "/proc/ppc64/pmc",
  "/proc/ppc64/pcifr",
);

#  Command output to include in all snaps
@snap_commands_general = (
  "lscfg -vp",
  "ifconfig -a",
);

#  Command output to include only in detailed snaps (-a option)
@snap_commands_detailed = (
  "rpm -qa",
);
  

sub recurse_dir($);		# function prototype

sub error {
	my ($fatal, $message) = @_;

	if ($fatal) {
		print "$0: $message\n";
		exit 2;
	}
	else {
		if ($opt_v) {
			print "$0: $message\n";
		}
	}
}

sub print_usage {
	print "Usage: $0 [-ahv] [-d dir] [-o file]\n\n";
	print "  Command-line parameters:\n";
	print "    a:       all data; collect detailed information (more files and output)\n";
	print "    d dir:   specify the directory where files and output will be collected\n"; 
	print "               (default: /tmp/ibmsupt)\n";
	print "    h:       print this help message\n";
	print "    o file:  specify the output file (.tar required, .tar.gz optional)\n";
	print "               (default: snap.tar.gz)\n";
	print "    v:       verbose output\n\n";
	print "  Exit codes (view with \"echo \$?\" immediately after running):\n";
	print "    0:  snap data was successfully captured\n";
	print "    1:  invalid command line\n";
	print "    2:  other fatal error\n\n";
}

sub copy {
	my ($source, $destination) = @_;
	my ($dir, @path, $d, $blocksize, $buffer, $length);

	#print "Copying $source...";

	# Create directories, if necessary
	$dir = substr $destination, 0, rindex($destination, "/");
	if (!(-d $dir)) {
		@path = split /\//, $dir;
		if (substr($dir, 0, 1) eq "/") {	# remove leading /
			shift @path;
		}
		$dir = "";
		foreach $d (@path) {
			$dir .= "/" . $d;
			if (!(-d $dir)) {
				if (!mkdir($dir, 0644)) {
					error(0, "Cannot create directory: $dir");
					return;
				}
			}
		}
	}

	# Copy file
	if (!open(SRC, "<$source")) {
		error(0, "Cannot open file for reading: $source");
		return;
	}
	binmode SRC;
	if (!open(DST, ">$destination")) {
		error(0, "Cannot open file for writing: $destination");
		goto copy_out;
	}
	binmode DST;

	$blocksize = (stat SRC)[11] || 16384;
	while ($length = sysread SRC, $buffer, $blocksize) {
		if (!defined $length) {
			next if $! =~ /^Interrupted/;	# ^Z and fg
			error(0, "System read error while reading $source: $!");
			goto copy_out;
		}
		$offset = 0;
		while ($length) {
			if (!defined($written = syswrite DST, $buffer, $length, $offset)) {
				error(0, "System write error while writing $destination: $!");
				goto copy_out;
			}
			$length -= $written;
			$offset += $written;
		}
	}

copy_out:
	#print "done.\n";
	close SRC;
	close DST;
}

sub recurse_dir ($) {
	my ($dir) = @_;
	my ($file) = "";
	my (@contents) = ();

	if (!opendir(DIR, $dir)) {
		error(0, "Could not open directory $dir");
		return;
	}

	@contents = readdir DIR;
	closedir DIR;

	foreach $file (@contents) {
		if ($file eq "." or $file eq ".." or (-l "$dir/$file")) {
			next;
		}

		if (-d "$dir/$file") {
			recurse_dir "$dir/$file";
		}
		else {
			copy "$dir/$file", $outdir."$dir/$file";
		}
	}
}

sub snap_paths {
	my ($file, $dir, $search, @contents);

	foreach $file (@_) {
		if (-d $file) {
			recurse_dir $file;
		}
		else {
			# Check for wildcard (* in last character only)
			if (substr($file, -1) eq "*") {
				$dir = substr $file, 0, rindex($file, "/");
				$search = substr $file, rindex($file, "/")+1, -1;

				if (!opendir(DIR, $dir)) {
					error(0, "Could not open directory $dir");
					return;
				}

				@contents = readdir DIR;
				closedir DIR;

				foreach $file (@contents) {
					if (substr($file, 0, length($search)) eq $search) {
						copy "$dir/$file", $outdir."$dir/$file";
					}
				}
			}
			else {
				copy $file, $outdir.$file;
			}
		}
	}
}

sub snap_commands {
	my ($path, @junk, @path, $filename, $command, $exit_value);

	foreach $command (@_) {
		# Retrieve the name of the binary to run (for output file name)
		($path, @junk) = split / /, $command;
		@path = reverse(split /\//, $path);
		$filename = shift @path;

		system("$command > $outdir/$filename.out 2>&1");
		if ($exit_value = $? >> 8) {
			error(0, "\"$command\" returned $exit_value");
		}
	}
}

$< == 0 or error(1, "Must be executed as root");

$opt_a = 0;
$opt_d = 0;
$opt_h = 0;
$opt_o = 0;
$opt_v = 0;
if (!getopts('ad:ho:v')) {
	print_usage;
	exit 1;
}

if ($opt_h) {
	print_usage;
	exit 0;
}

if ($opt_d) {
	$outdir = $opt_d;
	if ($outdir eq "/") {
		error(1, "Cannot use / as the output directory");
	}
	if (substr($outdir, -1) eq "/") {
		$outdir = substr $outdir, 0, -1;
	}
}
if ($opt_o) {
	if ($opt_o !~ /.tar/) {
		print "$0: The filename provided, $opt_o, does not contain .tar;  using default filename $outfile\n";
	}
	else {
		$outfile = $opt_o;
	}
}

snap_paths(@snap_paths_general);

# Check distro
if (-e "/etc/SuSE-release") {
	snap_paths(@snap_paths_general_SuSE);
}
elsif (-e "/etc/redhat-release") {
	snap_paths(@snap_paths_general_RedHat);
}

# Run commands and capture output
snap_commands(@snap_commands_general);

# Gather detail files if requested (-a option)
if ($opt_a) {
	snap_paths(@snap_paths_detailed);
	snap_commands(@snap_commands_detailed);
}

($basefile, $extension) = split /\.tar/, $outfile;

system ("tar -cf $basefile.tar $outdir 2>/dev/null");

if ($extension eq ".gz") {
	system ("gzip -f $basefile.tar");
}
elsif ($extension eq "") { }
else {
	$outfile = "$basefile.tar";
	print "$0: Unrecognized extension $extension\n";
}

print "output written to $outfile\n";
exit 0;