Improve packaging of this package: 1.) Handle installation of the script to determine the amount of free memory and swap space on the local machine automatically. 2.) Fix the NetBSD implementation of the above script. 3.) Create a wrapper shell script for invoking Cacti's poller. 4.) Simplify the installation instrunctions using the above enhancements. 5.) Don't included the log file in the package list. It doesn't belong there and "pkg_delete" will correctly complain that it has been modified. ToDo: - The log file and the "rrdtool" database still need to be moved to a directory under "${VARBASE}". - "config.php" should really be a config file to allow using a non-default password for the MySQL database. But the file would have to be readable by both the user of the webserver and that cacti user.diff -r0 -r1.1 pkgsrc/net/cacti/INSTALL
(tron)
# $NetBSD: INSTALL,v 1.1 2014/01/16 21:10:00 tron Exp $
case ${STAGE} in
POST-INSTALL)
${TOUCH} @CACTI_LOGDIR@/cacti.log
${CHOWN} cacti:cacti @CACTI_LOGDIR@/cacti.log
${CHMOD} 640 @CACTI_LOGDIR@/cacti.log
;;
esac
@@ -1,47 +1,41 @@ | @@ -1,47 +1,41 @@ | |||
1 | =========================================================================== | 1 | =========================================================================== | |
2 | $NetBSD: MESSAGE,v 1.1.1.1 2011/11/22 22:23:13 tez Exp $ | 2 | $NetBSD: MESSAGE,v 1.2 2014/01/16 21:10:00 tron Exp $ | |
3 | 3 | |||
4 | cacti is now installed. | 4 | cacti is now installed. | |
5 | 5 | |||
6 | To get cacti up and running asap, you have to complete these steps: | 6 | To get cacti up and running asap, you have to complete these steps: | |
7 | 7 | |||
8 | - Install & Configure mysql-server | 8 | - Install & Configure MySQL database server. | |
9 | 9 | |||
10 | - Add a cacti database and cactiuser user to mysql | 10 | - Add a cacti database and cactiuser user to MySQL and create the cacti | |
11 | database layout (${CACTIDIR}/cacti.sql) to MySQL: | |||
11 | 12 | |||
12 | mysql> CREATE DATABASE cacti; | 13 | mysql> CREATE DATABASE cacti; | |
13 | mysql> GRANT ALL PRIVILEGES ON cacti.* TO 'cactiuser'@'localhost' | 14 | mysql> GRANT ALL PRIVILEGES ON cacti.* TO 'cactiuser'@'localhost' | |
14 | -> IDENTIFIED BY 'cactiuser'; | 15 | -> IDENTIFIED BY 'cactiuser'; | |
16 | mysql> FLUSH PRIVILEGES; | |||
17 | mysql> USE cacti; | |||
18 | mysql> SOURCE ${CACTIDIR}/cacti.sql; | |||
15 | 19 | |||
16 | - Add the cacti database layout (${CACTIDIR}/cacti.sql) to mysql | 20 | - Install & Configure an Apache webserver. | |
17 | ||||
18 | shell> mysql [-p] cacti < ${CACTIDIR}/cacti.sql | |||
19 | ||||
20 | - Add a symlink from ${CACTIDIR}/scripts/local_memory.pl to the appropriate | |||
21 | *_memory.pl script (or create one if none for your OS exists) | |||
22 | ||||
23 | shell> ln -s ${CACTIDIR}/scripts/netbsd_memory.pl \ | |||
24 | ${CACTIDIR}/scripts/local_memory.pl | |||
25 | ||||
26 | - Install & Configure an apache webserver | |||
27 | 21 | |||
28 | - If you have not already done so, add these lines to enable php extensions | 22 | - If you have not already done so, add these lines to enable php extensions | |
29 | required by cacti in ${PKG_SYSCONFBASE}/etc/php.ini | 23 | required by cacti in ${PKG_SYSCONFBASE}/etc/php.ini | |
30 | 24 | |||
31 | extension=mysql.so | 25 | extension=mysql.so | |
32 | extension=snmp.so | 26 | extension=snmp.so | |
33 | extension=sockets.so | 27 | extension=sockets.so | |
34 | 28 | |||
35 | - Append this to apache's httpd.conf and restart apache | 29 | - Append this to Apache webserver's "httpd.conf" and restart the server. | |
36 | 30 | |||
37 | Include etc/httpd/httpd-cacti.conf | 31 | Include etc/httpd/httpd-cacti.conf | |
38 | 32 | |||
39 | - Add the following lines to ${CACTI_USER}'s crontab | 33 | - Add the following lines to ${CACTI_USER}'s crontab | |
40 | 34 | |||
41 | # Invoke poller.php to collect snmp-statistics | 35 | # Invoke poller.php to collect SNMP statistics. | |
42 | */5 * * * * ${PREFIX}/bin/php ${CACTIDIR}/poller.php > ${CACTI_LOGDIR}/poller.log 2>&1 | 36 | */5 * * * * ${PREFIX}/bin/cacti-poller | |
43 | 37 | |||
44 | Please read the included README (${CACTIDIR}/docs/README) file, | 38 | Please read the included README (${CACTIDIR}/docs/README) file, | |
45 | for more information about configuring and starting cacti. | 39 | for more information about configuring and starting cacti. | |
46 | 40 | |||
47 | =========================================================================== | 41 | =========================================================================== |
@@ -1,16 +1,17 @@ | @@ -1,16 +1,17 @@ | |||
1 | # $NetBSD: Makefile,v 1.21 2014/01/08 20:51:28 tron Exp $ | 1 | # $NetBSD: Makefile,v 1.22 2014/01/16 21:10:00 tron Exp $ | |
2 | 2 | |||
3 | DISTNAME= cacti-0.8.8b | 3 | DISTNAME= cacti-0.8.8b | |
4 | PKGREVISION= 1 | |||
4 | CATEGORIES= net | 5 | CATEGORIES= net | |
5 | MASTER_SITES= http://www.cacti.net/downloads/ | 6 | MASTER_SITES= http://www.cacti.net/downloads/ | |
6 | 7 | |||
7 | MAINTAINER= pkgsrc-users@NetBSD.org | 8 | MAINTAINER= pkgsrc-users@NetBSD.org | |
8 | HOMEPAGE= http://www.cacti.net/ | 9 | HOMEPAGE= http://www.cacti.net/ | |
9 | COMMENT= Frontend to rrdtool for monitoring systems and services | 10 | COMMENT= Frontend to rrdtool for monitoring systems and services | |
10 | LICENSE= gnu-gpl-v2 | 11 | LICENSE= gnu-gpl-v2 | |
11 | 12 | |||
12 | USE_LANGUAGES= # none | 13 | USE_LANGUAGES= # none | |
13 | USE_TOOLS+= pax | 14 | USE_TOOLS+= pax | |
14 | NO_BUILD= yes | 15 | NO_BUILD= yes | |
15 | 16 | |||
16 | DEPENDS+= ${PHP_PKG_PREFIX}-mysql-[0-9]*:../../databases/php-mysql | 17 | DEPENDS+= ${PHP_PKG_PREFIX}-mysql-[0-9]*:../../databases/php-mysql | |
@@ -36,53 +37,67 @@ EGDIR= ${PREFIX}/share/examples/cacti | @@ -36,53 +37,67 @@ EGDIR= ${PREFIX}/share/examples/cacti | |||
36 | CONF_FILES+= ${EGDIR}/httpd-cacti.conf ${PKG_SYSCONFDIR}/httpd-cacti.conf | 37 | CONF_FILES+= ${EGDIR}/httpd-cacti.conf ${PKG_SYSCONFDIR}/httpd-cacti.conf | |
37 | CACTIDIR= ${PREFIX}/share/cacti | 38 | CACTIDIR= ${PREFIX}/share/cacti | |
38 | CACTI_LOGDIR?= ${CACTIDIR}/log | 39 | CACTI_LOGDIR?= ${CACTIDIR}/log | |
39 | CACTI_RRADIR?= ${CACTIDIR}/rra | 40 | CACTI_RRADIR?= ${CACTIDIR}/rra | |
40 | 41 | |||
41 | REPLACE_INTERPRETER+= php | 42 | REPLACE_INTERPRETER+= php | |
42 | REPLACE.php.old= .*php[^ ]* | 43 | REPLACE.php.old= .*php[^ ]* | |
43 | REPLACE.php.new= ${PREFIX}/bin/php | 44 | REPLACE.php.new= ${PREFIX}/bin/php | |
44 | REPLACE_FILES.php= cli/*.php | 45 | REPLACE_FILES.php= cli/*.php | |
45 | 46 | |||
46 | REPLACE_PERL+= scripts/*.pl | 47 | REPLACE_PERL+= scripts/*.pl | |
47 | 48 | |||
48 | MESSAGE_SUBST+= CACTIDIR=${CACTIDIR} | 49 | MESSAGE_SUBST+= CACTIDIR=${CACTIDIR} | |
49 | MESSAGE_SUBST+= CACTI_LOGDIR=${CACTI_LOGDIR} | |||
50 | MESSAGE_SUBST+= CACTI_USER=${CACTI_USER} | 50 | MESSAGE_SUBST+= CACTI_USER=${CACTI_USER} | |
51 | MESSAGE_SUBST+= EGDIR=${EGDIR} | 51 | MESSAGE_SUBST+= EGDIR=${EGDIR} | |
52 | MESSAGE_SUBST+= PREFIX=${PREFIX} | 52 | MESSAGE_SUBST+= PREFIX=${PREFIX} | |
53 | MESSAGE_SUBST+= PKG_SYSCONFBASE=${PKG_SYSCONFBASE} | 53 | MESSAGE_SUBST+= PKG_SYSCONFBASE=${PKG_SYSCONFBASE} | |
54 | FILES_SUBST+= CACTIDIR=${CACTIDIR} | 54 | FILES_SUBST+= CACTIDIR=${CACTIDIR} | |
55 | FILES_SUBST+= CACTI_GROUP=${CACTI_GROUP} | |||
56 | FILES_SUBST+= CACTI_USER=${CACTI_USER} | |||
57 | FILES_SUBST+= CACTI_LOGDIR=${CACTI_LOGDIR} | |||
55 | 58 | |||
56 | SUBST_CLASSES+= paths | 59 | SUBST_CLASSES+= paths | |
57 | SUBST_STAGE.paths= pre-configure | 60 | SUBST_STAGE.paths= pre-configure | |
58 | SUBST_FILES.paths= ${WRKDIR}/httpd-cacti.conf install/index.php | 61 | SUBST_FILES.paths= ${WRKDIR}/httpd-cacti.conf install/index.php | |
59 | SUBST_FILES.paths+= include/global.php include/global_settings.php | 62 | SUBST_FILES.paths+= include/global.php include/global_settings.php | |
63 | SUBST_FILES.paths+= ${WRKDIR}/cacti-poller | |||
60 | SUBST_VARS.paths= CACTIDIR PREFIX CACTI_USER CACTI_LOGDIR CACTI_RRADIR | 64 | SUBST_VARS.paths= CACTIDIR PREFIX CACTI_USER CACTI_LOGDIR CACTI_RRADIR | |
61 | SUBST_VARS.paths+= PKG_PHP_MAJOR_VERS | 65 | SUBST_VARS.paths+= PKG_PHP_MAJOR_VERS SH | |
62 | 66 | |||
63 | SUBST_CLASSES+= docs | 67 | SUBST_CLASSES+= docs | |
64 | SUBST_STAGE.docs= pre-configure | 68 | SUBST_STAGE.docs= pre-configure | |
65 | SUBST_FILES.docs= docs/txt/manual.txt docs/html/*.html | 69 | SUBST_FILES.docs= docs/txt/manual.txt docs/html/*.html | |
66 | SUBST_SED.docs= -e 's,/var/www/html/cacti,${CACTIDIR},g' | 70 | SUBST_SED.docs= -e 's,/var/www/html/cacti,${CACTIDIR},g' | |
67 | 71 | |||
68 | OWN_DIRS_PERMS+= ${CACTI_LOGDIR} ${CACTI_USER} ${CACTI_GROUP} 755 | 72 | OWN_DIRS_PERMS+= ${CACTI_LOGDIR} ${CACTI_USER} ${CACTI_GROUP} 755 | |
69 | OWN_DIRS_PERMS+= ${CACTI_RRADIR} ${CACTI_USER} ${CACTI_GROUP} 755 | 73 | OWN_DIRS_PERMS+= ${CACTI_RRADIR} ${CACTI_USER} ${CACTI_GROUP} 755 | |
70 | SPECIAL_PERMS+= share/cacti/log/cacti.log ${CACTI_USER} ${CACTI_GROUP} 755 | |||
71 | 74 | |||
72 | INSTALLATION_DIRS= share/examples/cacti | 75 | INSTALLATION_DIRS= bin | |
76 | INSTALLATION_DIRS+= share/examples/cacti | |||
73 | INSTALLATION_DIRS+= share/cacti | 77 | INSTALLATION_DIRS+= share/cacti | |
74 | 78 | |||
79 | PLIST_VARS+= localmemory | |||
80 | .if ${OPSYS} == "FreeBSD" || ${OPSYS} == "Linux" || \ | |||
81 | ${OPSYS} == "NetBSD" || ${OPSYS} == "SunOS" | |||
82 | PLIST.localmemory= yes | |||
83 | .endif | |||
84 | ||||
75 | post-extract: | 85 | post-extract: | |
76 | ${CP} ${FILESDIR}/httpd-cacti.conf ${WRKDIR}/ | 86 | ${CP} ${FILESDIR}/httpd-cacti.conf ${WRKDIR}/ | |
87 | ${CP} ${FILESDIR}/cacti-poller ${WRKDIR}/ | |||
77 | ${CP} ${FILESDIR}/freebsd_memory.pl ${WRKSRC}/scripts/ | 88 | ${CP} ${FILESDIR}/freebsd_memory.pl ${WRKSRC}/scripts/ | |
78 | ${CP} ${FILESDIR}/netbsd_memory.pl ${WRKSRC}/scripts/ | 89 | ${CP} ${FILESDIR}/netbsd_memory.pl ${WRKSRC}/scripts/ | |
79 | ${CP} ${FILESDIR}/solaris_memory.pl ${WRKSRC}/scripts/ | 90 | ${CP} ${FILESDIR}/solaris_memory.pl ${WRKSRC}/scripts/ | |
91 | cd ${WRKSRC}/scripts; \ | |||
92 | if [ -f ${LOWER_OPSYS}_memory.pl ]; then \ | |||
93 | ${LN} -fs ${LOWER_OPSYS}_memory.pl local_memory.pl; \ | |||
94 | fi | |||
80 | 95 | |||
81 | do-install: | 96 | do-install: | |
82 | ${INSTALL_DATA} ${WRKDIR}/httpd-cacti.conf ${DESTDIR}${EGDIR}/httpd-cacti.conf | 97 | ${INSTALL_DATA} ${WRKDIR}/httpd-cacti.conf ${DESTDIR}${EGDIR}/httpd-cacti.conf | |
83 | ${FIND} ${WRKSRC} -type f -name '*.orig' -exec ${RM} -f {} \; | 98 | ${FIND} ${WRKSRC} -type f -name '*.orig' -exec ${RM} -f {} \; | |
84 | cd ${WRKSRC} && ${PAX} -wr * ${DESTDIR}${CACTIDIR} | 99 | cd ${WRKSRC} && ${PAX} -wr * ${DESTDIR}${CACTIDIR} | |
85 | 100 | ${INSTALL_SCRIPT} ${WRKDIR}/cacti-poller ${DESTDIR}${PREFIX}/bin | ||
86 | 101 | |||
87 | .include "../../databases/rrdtool/buildlink3.mk" | 102 | .include "../../databases/rrdtool/buildlink3.mk" | |
88 | .include "../../mk/bsd.pkg.mk" | 103 | .include "../../mk/bsd.pkg.mk" |
@@ -1,14 +1,15 @@ | @@ -1,14 +1,15 @@ | |||
1 | @comment $NetBSD: PLIST,v 1.4 2014/01/08 20:51:28 tron Exp $ | 1 | @comment $NetBSD: PLIST,v 1.5 2014/01/16 21:10:00 tron Exp $ | |
2 | bin/cacti-poller | |||
2 | share/cacti/LICENSE | 3 | share/cacti/LICENSE | |
3 | share/cacti/README | 4 | share/cacti/README | |
4 | share/cacti/about.php | 5 | share/cacti/about.php | |
5 | share/cacti/auth_changepassword.php | 6 | share/cacti/auth_changepassword.php | |
6 | share/cacti/auth_login.php | 7 | share/cacti/auth_login.php | |
7 | share/cacti/cacti.sql | 8 | share/cacti/cacti.sql | |
8 | share/cacti/cdef.php | 9 | share/cacti/cdef.php | |
9 | share/cacti/cli/.htaccess | 10 | share/cacti/cli/.htaccess | |
10 | share/cacti/cli/add_data_query.php | 11 | share/cacti/cli/add_data_query.php | |
11 | share/cacti/cli/add_device.php | 12 | share/cacti/cli/add_device.php | |
12 | share/cacti/cli/add_graph_template.php | 13 | share/cacti/cli/add_graph_template.php | |
13 | share/cacti/cli/add_graphs.php | 14 | share/cacti/cli/add_graphs.php | |
14 | share/cacti/cli/add_perms.php | 15 | share/cacti/cli/add_perms.php | |
@@ -434,27 +435,26 @@ share/cacti/lib/ping.php | @@ -434,27 +435,26 @@ share/cacti/lib/ping.php | |||
434 | share/cacti/lib/plugins.php | 435 | share/cacti/lib/plugins.php | |
435 | share/cacti/lib/poller.php | 436 | share/cacti/lib/poller.php | |
436 | share/cacti/lib/rrd.php | 437 | share/cacti/lib/rrd.php | |
437 | share/cacti/lib/snmp.php | 438 | share/cacti/lib/snmp.php | |
438 | share/cacti/lib/sort.php | 439 | share/cacti/lib/sort.php | |
439 | share/cacti/lib/template.php | 440 | share/cacti/lib/template.php | |
440 | share/cacti/lib/time.php | 441 | share/cacti/lib/time.php | |
441 | share/cacti/lib/timespan_settings.php | 442 | share/cacti/lib/timespan_settings.php | |
442 | share/cacti/lib/tree.php | 443 | share/cacti/lib/tree.php | |
443 | share/cacti/lib/utility.php | 444 | share/cacti/lib/utility.php | |
444 | share/cacti/lib/variables.php | 445 | share/cacti/lib/variables.php | |
445 | share/cacti/lib/xml.php | 446 | share/cacti/lib/xml.php | |
446 | share/cacti/log/.htaccess | 447 | share/cacti/log/.htaccess | |
447 | share/cacti/log/cacti.log | |||
448 | share/cacti/logout.php | 448 | share/cacti/logout.php | |
449 | share/cacti/plugins.php | 449 | share/cacti/plugins.php | |
450 | share/cacti/plugins/index.php | 450 | share/cacti/plugins/index.php | |
451 | share/cacti/poller.php | 451 | share/cacti/poller.php | |
452 | share/cacti/poller_commands.php | 452 | share/cacti/poller_commands.php | |
453 | share/cacti/poller_export.php | 453 | share/cacti/poller_export.php | |
454 | share/cacti/resource/script_queries/host_cpu.xml | 454 | share/cacti/resource/script_queries/host_cpu.xml | |
455 | share/cacti/resource/script_queries/host_disk.xml | 455 | share/cacti/resource/script_queries/host_disk.xml | |
456 | share/cacti/resource/script_queries/unix_disk.xml | 456 | share/cacti/resource/script_queries/unix_disk.xml | |
457 | share/cacti/resource/script_server/host_cpu.xml | 457 | share/cacti/resource/script_server/host_cpu.xml | |
458 | share/cacti/resource/script_server/host_disk.xml | 458 | share/cacti/resource/script_server/host_disk.xml | |
459 | share/cacti/resource/snmp_queries/host_disk.xml | 459 | share/cacti/resource/snmp_queries/host_disk.xml | |
460 | share/cacti/resource/snmp_queries/interface.xml | 460 | share/cacti/resource/snmp_queries/interface.xml | |
@@ -463,26 +463,27 @@ share/cacti/resource/snmp_queries/net-sn | @@ -463,26 +463,27 @@ share/cacti/resource/snmp_queries/net-sn | |||
463 | share/cacti/resource/snmp_queries/netware_cpu.xml | 463 | share/cacti/resource/snmp_queries/netware_cpu.xml | |
464 | share/cacti/resource/snmp_queries/netware_disk.xml | 464 | share/cacti/resource/snmp_queries/netware_disk.xml | |
465 | share/cacti/rra.php | 465 | share/cacti/rra.php | |
466 | share/cacti/rra/.htaccess | 466 | share/cacti/rra/.htaccess | |
467 | share/cacti/script_server.php | 467 | share/cacti/script_server.php | |
468 | share/cacti/script_server.pl | 468 | share/cacti/script_server.pl | |
469 | share/cacti/scripts/3com_cable_modem.pl | 469 | share/cacti/scripts/3com_cable_modem.pl | |
470 | share/cacti/scripts/diskfree.pl | 470 | share/cacti/scripts/diskfree.pl | |
471 | share/cacti/scripts/diskfree.sh | 471 | share/cacti/scripts/diskfree.sh | |
472 | share/cacti/scripts/freebsd_memory.pl | 472 | share/cacti/scripts/freebsd_memory.pl | |
473 | share/cacti/scripts/linux_memory.pl | 473 | share/cacti/scripts/linux_memory.pl | |
474 | share/cacti/scripts/loadavg.pl | 474 | share/cacti/scripts/loadavg.pl | |
475 | share/cacti/scripts/loadavg_multi.pl | 475 | share/cacti/scripts/loadavg_multi.pl | |
476 | ${PLIST.localmemory}share/cacti/scripts/local_memory.pl | |||
476 | share/cacti/scripts/netbsd_memory.pl | 477 | share/cacti/scripts/netbsd_memory.pl | |
477 | share/cacti/scripts/ping.pl | 478 | share/cacti/scripts/ping.pl | |
478 | share/cacti/scripts/query_host_cpu.php | 479 | share/cacti/scripts/query_host_cpu.php | |
479 | share/cacti/scripts/query_host_partitions.php | 480 | share/cacti/scripts/query_host_partitions.php | |
480 | share/cacti/scripts/query_unix_partitions.pl | 481 | share/cacti/scripts/query_unix_partitions.pl | |
481 | share/cacti/scripts/solaris_memory.pl | 482 | share/cacti/scripts/solaris_memory.pl | |
482 | share/cacti/scripts/sql.php | 483 | share/cacti/scripts/sql.php | |
483 | share/cacti/scripts/ss_fping.php | 484 | share/cacti/scripts/ss_fping.php | |
484 | share/cacti/scripts/ss_host_cpu.php | 485 | share/cacti/scripts/ss_host_cpu.php | |
485 | share/cacti/scripts/ss_host_disk.php | 486 | share/cacti/scripts/ss_host_disk.php | |
486 | share/cacti/scripts/ss_sql.php | 487 | share/cacti/scripts/ss_sql.php | |
487 | share/cacti/scripts/unix_processes.pl | 488 | share/cacti/scripts/unix_processes.pl | |
488 | share/cacti/scripts/unix_tcp_connections.pl | 489 | share/cacti/scripts/unix_tcp_connections.pl |
#!@SH@
#
# $NetBSD: cacti-poller,v 1.1 2014/01/16 21:10:00 tron Exp $
#
LOGFILE=@CACTI_LOGDIR@/cacti.log
exec @PREFIX@/bin/php @CACTIDIR@/poller.php >>$LOGFILE 2>&1
@@ -1,23 +1,99 @@ | @@ -1,23 +1,99 @@ | |||
1 | #!/usr/bin/perl | 1 | #!/usr/bin/perl | |
2 | # | |||
3 | # $NetBSD: netbsd_memory.pl,v 1.2 2014/01/16 21:10:00 tron Exp $ | |||
4 | # | |||
5 | # Copyright (c) 2014 The NetBSD Foundation, Inc. | |||
6 | # All rights reserved. | |||
7 | # | |||
8 | # This code is derived from software contributed to The NetBSD Foundation | |||
9 | # by Matthias Scheler. | |||
10 | # | |||
11 | # Redistribution and use in source and binary forms, with or without | |||
12 | # modification, are permitted provided that the following conditions | |||
13 | # are met: | |||
14 | # 1. Redistributions of source code must retain the above copyright | |||
15 | # notice, this list of conditions and the following disclaimer. | |||
16 | # 2. Redistributions in binary form must reproduce the above copyright | |||
17 | # notice, this list of conditions and the following disclaimer in the | |||
18 | # documentation and/or other materials provided with the distribution. | |||
19 | # | |||
20 | # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |||
21 | # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |||
22 | # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |||
23 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |||
24 | # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |||
25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |||
26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |||
27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |||
28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |||
30 | # POSSIBILITY OF SUCH DAMAGE. | |||
31 | # | |||
2 | 32 | |||
3 | if (($ARGV[0] =~ /Cached:/) || ($ARGV[0] =~ /SwapFree:/)) { | 33 | use warnings; | |
4 | open(PROCESS, "pstat -ks |"); | 34 | use strict; | |
5 | $s = 0; | 35 | ||
6 | while (<PROCESS>) { | 36 | use POSIX; | |
7 | if (!/^Device/) { | 37 | ||
8 | split(); | 38 | # Use vmstat(1) to get memory usage information. "vmstat -s" produces | |
9 | $s += $_[3]; | 39 | # output lines like these: | |
10 | } | 40 | # | |
11 | } | 41 | # 2034763 pages managed | |
12 | print "$s"; | 42 | # 28953 pages free | |
13 | } | 43 | # | |
14 | else { # $ARGV[0] is Buffers:, MemFree:, or anything else | 44 | # This function creates a hash using the description as a key to the value. | |
15 | open(PROCESS, "vmstat |"); | 45 | ||
16 | while (<PROCESS>) { | 46 | sub get_vm_stats() | |
17 | if (!/p/) { # the numbers line happens not to have "p" | 47 | { | |
18 | split(); | 48 | my %vm_stats; | |
19 | print("$_[4]"); | 49 | ||
20 | } | 50 | my $vmstat_fh; | |
21 | } | 51 | if (!open($vmstat_fh, '-|', '/usr/bin/vmstat -s')) { | |
22 | } | 52 | die('Cannot start vmstat.'); | |
23 | close(PROCESS); | 53 | } | |
54 | ||||
55 | while (my $line = <$vmstat_fh>) { | |||
56 | $line =~ s/^\s+//; | |||
57 | $line =~ s/\s+$//; | |||
58 | my @words = split(/\s+/, $line, 2); | |||
59 | if (scalar(@words) < 2) { | |||
60 | next; | |||
61 | } | |||
62 | my ($value, $descr) = @words; | |||
63 | ||||
64 | $vm_stats{$descr} = $value; | |||
65 | } | |||
66 | ||||
67 | close($vmstat_fh); | |||
68 | return %vm_stats; | |||
69 | } | |||
70 | ||||
71 | if (scalar(@ARGV) < 1) { | |||
72 | die('Please provide a field name'); | |||
73 | } | |||
74 | ||||
75 | my $page_size = POSIX::sysconf(&POSIX::_SC_PAGESIZE); | |||
76 | my %vm_stats = get_vm_stats(); | |||
77 | ||||
78 | # Calculate metrics using the stats reported by vmstat(1). | |||
79 | # Cacti only uses "MemFree" and "SwapFree" at the moment. | |||
80 | my $value = 0; | |||
81 | if ($ARGV[0] =~ 'MemTotal') { | |||
82 | $value = $vm_stats{'pages managed'}; | |||
83 | } elsif ($ARGV[0] =~ 'MemFree') { | |||
84 | $value = $vm_stats{'pages free'}; | |||
85 | } elsif ($ARGV[0] =~ 'Buffers') { | |||
86 | $value = $vm_stats{'cached file pages'}; | |||
87 | } elsif ($ARGV[0] =~ 'Cached') { | |||
88 | $value = $vm_stats{'anonymous pages'} + | |||
89 | $vm_stats{'cached file pages'} + | |||
90 | $vm_stats{'cached executable pages'}; | |||
91 | } elsif ($ARGV[0] =~ 'SwapTotal') { | |||
92 | $value = $vm_stats{'swap pages'}; | |||
93 | } elsif ($ARGV[0] =~ 'SwapFree') { | |||
94 | $value = $vm_stats{'swap pages'} - | |||
95 | $vm_stats{'swap pages in use'}; | |||
96 | } | |||
97 | ||||
98 | print $value * ($page_size / 1024); | |||
99 | exit(0); |