File: check_plugins.pl

package info (click to toggle)
collectd 5.12.0-27
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,160 kB
  • sloc: ansic: 120,848; perl: 20,684; php: 3,007; makefile: 2,441; java: 1,713; javascript: 920; python: 892; sh: 799; cpp: 638; yacc: 231; sql: 198; lex: 146; ruby: 49; xml: 44
file content (158 lines) | stat: -rwxr-xr-x 3,650 bytes parent folder | download | duplicates (5)
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
#! /usr/bin/perl --
#
# collectd - check_plugins.pl
# Copyright (C) 2006, 2007 Sebastian Harl
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; only version 2 of the License is applicable.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# Author:
#   Sebastian Harl <sh at tokkee.org>

# This script checks each plugin and reports the address of each plugin's
# registered functions. It uses src/.libs/*.so for its checks.  This can be
# used to check what kind of operations each plugin supports after it has been
# built.

use strict;
use warnings;

my $srcdir = 'src/';
my $libdir = '.libs';

my $plugins = {};

my ($srcs, $libs) = (undef, undef);

if (! opendir($srcs, $srcdir)) {
	print STDERR "Could not open directory '$srcdir': $!\n"
		. "Make sure you are in the toplevel source directory.\n";
	exit 1;
}

while (my $dirent = readdir($srcs)) {
	if ($dirent !~ m/^(.*)\.c$/) {
		next;
	}

	my $name = $1;
	my $src  = undef;

	if (! open($src, "<", "$srcdir/$dirent")) {
		print STDERR "Unable to open '$srcdir/$dirent': $!\n";
		next;
	}

	while (my $line = <$src>) {
		if ($line =~ m/plugin_register_(\w+)\s*\("([^"]+)",\s*(\w+)/) {
			my ($t, $n, $f) = ($1, $2, $3);

			$plugins->{$name}->{$n}->{$t} = $f;
		}
	}

	close($src);
} # while (my $dirent = readdir($srcs))

closedir($srcs);

if (! opendir($libs, $libdir)) {
	print STDERR "Could not open directory '$libdir': $!\n"
		. "Make sure you ran 'make'.\n";
	exit 1;
}

while (my $dirent = readdir($libs)) {
	if ($dirent !~ m/^(.*)\.so$/) {
		next;
	}

	my $name = $1;
	my $nm   = undef;

	if (! defined $plugins->{$name}) {
		print STDERR "No information available for plugin '$name'!\n";
		next;
	}

	if (! open($nm, "-|", "nm $libdir/$dirent")) {
		print STDERR "Unable to open pipe from nm(1): $!\n";
		next;
	}

	while (my $line = <$nm>) {
		if ($line !~ m/^([0-9a-fA-F]{8,}) [tT] (\w+)$/) {
			next;
		}

		my $adr = $1;
		my $sym = $2;

		for my $n (keys %{$plugins->{$name}}) {
			for my $t (keys %{$plugins->{$name}->{$n}}) {
				if (defined $plugins->{$name}->{$n}->{$t}
						&& ($sym eq $plugins->{$name}->{$n}->{$t})) {
					$plugins->{$name}->{$n}->{$t} = "0x" . $adr;
				}
			}
		}
	}

	close($nm);
} # while (my $dirent = readdir($libs))

closedir($libs);

print 'plugin name     config   init     read     write    log      shutdown';
print $/ . '-' x 70 . $/;

for my $name (sort keys %$plugins) {
	if (! -f "$libdir/$name.so") {
		print "$name.c has not been compiled.\n";
		next;
	}

	for my $n (sort keys %{$plugins->{$name}}) {
		dump_plugin_data($n, $plugins->{$name}->{$n});
	}
}

exit 0;

sub dump_plugin_data {
	my $name  = shift || return;
	my $funcs = shift || return;

	if (length($name) > 15) {
		$name = substr($name, 0, 12) . '...';
	}

	printf '%-15s ', $name;

	foreach my $t ("config", "init", "read", "write", "log", "shutdown") {
		if (! defined $funcs->{$t}) {
			print '-        ';
		}
		elsif ($funcs->{$t} =~ m/^0x[A-Fa-f0-9]{8,}$/) {
			print substr($funcs->{$t}, -8, 8) . " ";
		}
		else {
			print 'nA       ';
		}
	}

	print $/;
	return 1;
} # sub dump_plugin_data

# vim: set sw=4 ts=4 tw=78 noexpandtab :