Thu Jan 16 21:10:00 2014 UTC ()
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.


(tron)
diff -r0 -r1.1 pkgsrc/net/cacti/INSTALL
diff -r1.1.1.1 -r1.2 pkgsrc/net/cacti/MESSAGE
diff -r1.21 -r1.22 pkgsrc/net/cacti/Makefile
diff -r1.4 -r1.5 pkgsrc/net/cacti/PLIST
diff -r0 -r1.1 pkgsrc/net/cacti/files/cacti-poller
diff -r1.1.1.1 -r1.2 pkgsrc/net/cacti/files/netbsd_memory.pl

File Added: pkgsrc/net/cacti/INSTALL
# $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

cvs diff -r1.1.1.1 -r1.2 pkgsrc/net/cacti/MESSAGE (expand / switch to unified diff)

--- pkgsrc/net/cacti/MESSAGE 2011/11/22 22:23:13 1.1.1.1
+++ pkgsrc/net/cacti/MESSAGE 2014/01/16 21:10:00 1.2
@@ -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
4cacti is now installed. 4cacti is now installed.
5 5
6To get cacti up and running asap, you have to complete these steps: 6To 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
44Please read the included README (${CACTIDIR}/docs/README) file, 38Please read the included README (${CACTIDIR}/docs/README) file,
45for more information about configuring and starting cacti. 39for more information about configuring and starting cacti.
46 40
47=========================================================================== 41===========================================================================

cvs diff -r1.21 -r1.22 pkgsrc/net/cacti/Makefile (expand / switch to unified diff)

--- pkgsrc/net/cacti/Makefile 2014/01/08 20:51:28 1.21
+++ pkgsrc/net/cacti/Makefile 2014/01/16 21:10:00 1.22
@@ -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
3DISTNAME= cacti-0.8.8b 3DISTNAME= cacti-0.8.8b
 4PKGREVISION= 1
4CATEGORIES= net 5CATEGORIES= net
5MASTER_SITES= http://www.cacti.net/downloads/ 6MASTER_SITES= http://www.cacti.net/downloads/
6 7
7MAINTAINER= pkgsrc-users@NetBSD.org 8MAINTAINER= pkgsrc-users@NetBSD.org
8HOMEPAGE= http://www.cacti.net/ 9HOMEPAGE= http://www.cacti.net/
9COMMENT= Frontend to rrdtool for monitoring systems and services 10COMMENT= Frontend to rrdtool for monitoring systems and services
10LICENSE= gnu-gpl-v2 11LICENSE= gnu-gpl-v2
11 12
12USE_LANGUAGES= # none 13USE_LANGUAGES= # none
13USE_TOOLS+= pax 14USE_TOOLS+= pax
14NO_BUILD= yes 15NO_BUILD= yes
15 16
16DEPENDS+= ${PHP_PKG_PREFIX}-mysql-[0-9]*:../../databases/php-mysql 17DEPENDS+= ${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
36CONF_FILES+= ${EGDIR}/httpd-cacti.conf ${PKG_SYSCONFDIR}/httpd-cacti.conf 37CONF_FILES+= ${EGDIR}/httpd-cacti.conf ${PKG_SYSCONFDIR}/httpd-cacti.conf
37CACTIDIR= ${PREFIX}/share/cacti 38CACTIDIR= ${PREFIX}/share/cacti
38CACTI_LOGDIR?= ${CACTIDIR}/log 39CACTI_LOGDIR?= ${CACTIDIR}/log
39CACTI_RRADIR?= ${CACTIDIR}/rra 40CACTI_RRADIR?= ${CACTIDIR}/rra
40 41
41REPLACE_INTERPRETER+= php 42REPLACE_INTERPRETER+= php
42REPLACE.php.old= .*php[^ ]* 43REPLACE.php.old= .*php[^ ]*
43REPLACE.php.new= ${PREFIX}/bin/php 44REPLACE.php.new= ${PREFIX}/bin/php
44REPLACE_FILES.php= cli/*.php 45REPLACE_FILES.php= cli/*.php
45 46
46REPLACE_PERL+= scripts/*.pl 47REPLACE_PERL+= scripts/*.pl
47 48
48MESSAGE_SUBST+= CACTIDIR=${CACTIDIR} 49MESSAGE_SUBST+= CACTIDIR=${CACTIDIR}
49MESSAGE_SUBST+= CACTI_LOGDIR=${CACTI_LOGDIR} 
50MESSAGE_SUBST+= CACTI_USER=${CACTI_USER} 50MESSAGE_SUBST+= CACTI_USER=${CACTI_USER}
51MESSAGE_SUBST+= EGDIR=${EGDIR} 51MESSAGE_SUBST+= EGDIR=${EGDIR}
52MESSAGE_SUBST+= PREFIX=${PREFIX} 52MESSAGE_SUBST+= PREFIX=${PREFIX}
53MESSAGE_SUBST+= PKG_SYSCONFBASE=${PKG_SYSCONFBASE} 53MESSAGE_SUBST+= PKG_SYSCONFBASE=${PKG_SYSCONFBASE}
54FILES_SUBST+= CACTIDIR=${CACTIDIR} 54FILES_SUBST+= CACTIDIR=${CACTIDIR}
 55FILES_SUBST+= CACTI_GROUP=${CACTI_GROUP}
 56FILES_SUBST+= CACTI_USER=${CACTI_USER}
 57FILES_SUBST+= CACTI_LOGDIR=${CACTI_LOGDIR}
55 58
56SUBST_CLASSES+= paths 59SUBST_CLASSES+= paths
57SUBST_STAGE.paths= pre-configure 60SUBST_STAGE.paths= pre-configure
58SUBST_FILES.paths= ${WRKDIR}/httpd-cacti.conf install/index.php 61SUBST_FILES.paths= ${WRKDIR}/httpd-cacti.conf install/index.php
59SUBST_FILES.paths+= include/global.php include/global_settings.php 62SUBST_FILES.paths+= include/global.php include/global_settings.php
 63SUBST_FILES.paths+= ${WRKDIR}/cacti-poller
60SUBST_VARS.paths= CACTIDIR PREFIX CACTI_USER CACTI_LOGDIR CACTI_RRADIR 64SUBST_VARS.paths= CACTIDIR PREFIX CACTI_USER CACTI_LOGDIR CACTI_RRADIR
61SUBST_VARS.paths+= PKG_PHP_MAJOR_VERS 65SUBST_VARS.paths+= PKG_PHP_MAJOR_VERS SH
62 66
63SUBST_CLASSES+= docs 67SUBST_CLASSES+= docs
64SUBST_STAGE.docs= pre-configure 68SUBST_STAGE.docs= pre-configure
65SUBST_FILES.docs= docs/txt/manual.txt docs/html/*.html 69SUBST_FILES.docs= docs/txt/manual.txt docs/html/*.html
66SUBST_SED.docs= -e 's,/var/www/html/cacti,${CACTIDIR},g' 70SUBST_SED.docs= -e 's,/var/www/html/cacti,${CACTIDIR},g'
67 71
68OWN_DIRS_PERMS+= ${CACTI_LOGDIR} ${CACTI_USER} ${CACTI_GROUP} 755 72OWN_DIRS_PERMS+= ${CACTI_LOGDIR} ${CACTI_USER} ${CACTI_GROUP} 755
69OWN_DIRS_PERMS+= ${CACTI_RRADIR} ${CACTI_USER} ${CACTI_GROUP} 755 73OWN_DIRS_PERMS+= ${CACTI_RRADIR} ${CACTI_USER} ${CACTI_GROUP} 755
70SPECIAL_PERMS+= share/cacti/log/cacti.log ${CACTI_USER} ${CACTI_GROUP} 755 
71 74
72INSTALLATION_DIRS= share/examples/cacti 75INSTALLATION_DIRS= bin
 76INSTALLATION_DIRS+= share/examples/cacti
73INSTALLATION_DIRS+= share/cacti 77INSTALLATION_DIRS+= share/cacti
74 78
 79PLIST_VARS+= localmemory
 80.if ${OPSYS} == "FreeBSD" || ${OPSYS} == "Linux" || \
 81 ${OPSYS} == "NetBSD" || ${OPSYS} == "SunOS"
 82PLIST.localmemory= yes
 83.endif
 84
75post-extract: 85post-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
81do-install: 96do-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"

cvs diff -r1.4 -r1.5 pkgsrc/net/cacti/PLIST (expand / switch to unified diff)

--- pkgsrc/net/cacti/PLIST 2014/01/08 20:51:28 1.4
+++ pkgsrc/net/cacti/PLIST 2014/01/16 21:10:00 1.5
@@ -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 $
 2bin/cacti-poller
2share/cacti/LICENSE 3share/cacti/LICENSE
3share/cacti/README 4share/cacti/README
4share/cacti/about.php 5share/cacti/about.php
5share/cacti/auth_changepassword.php 6share/cacti/auth_changepassword.php
6share/cacti/auth_login.php 7share/cacti/auth_login.php
7share/cacti/cacti.sql 8share/cacti/cacti.sql
8share/cacti/cdef.php 9share/cacti/cdef.php
9share/cacti/cli/.htaccess 10share/cacti/cli/.htaccess
10share/cacti/cli/add_data_query.php 11share/cacti/cli/add_data_query.php
11share/cacti/cli/add_device.php 12share/cacti/cli/add_device.php
12share/cacti/cli/add_graph_template.php 13share/cacti/cli/add_graph_template.php
13share/cacti/cli/add_graphs.php 14share/cacti/cli/add_graphs.php
14share/cacti/cli/add_perms.php 15share/cacti/cli/add_perms.php
@@ -434,27 +435,26 @@ share/cacti/lib/ping.php @@ -434,27 +435,26 @@ share/cacti/lib/ping.php
434share/cacti/lib/plugins.php 435share/cacti/lib/plugins.php
435share/cacti/lib/poller.php 436share/cacti/lib/poller.php
436share/cacti/lib/rrd.php 437share/cacti/lib/rrd.php
437share/cacti/lib/snmp.php 438share/cacti/lib/snmp.php
438share/cacti/lib/sort.php 439share/cacti/lib/sort.php
439share/cacti/lib/template.php 440share/cacti/lib/template.php
440share/cacti/lib/time.php 441share/cacti/lib/time.php
441share/cacti/lib/timespan_settings.php 442share/cacti/lib/timespan_settings.php
442share/cacti/lib/tree.php 443share/cacti/lib/tree.php
443share/cacti/lib/utility.php 444share/cacti/lib/utility.php
444share/cacti/lib/variables.php 445share/cacti/lib/variables.php
445share/cacti/lib/xml.php 446share/cacti/lib/xml.php
446share/cacti/log/.htaccess 447share/cacti/log/.htaccess
447share/cacti/log/cacti.log 
448share/cacti/logout.php 448share/cacti/logout.php
449share/cacti/plugins.php 449share/cacti/plugins.php
450share/cacti/plugins/index.php 450share/cacti/plugins/index.php
451share/cacti/poller.php 451share/cacti/poller.php
452share/cacti/poller_commands.php 452share/cacti/poller_commands.php
453share/cacti/poller_export.php 453share/cacti/poller_export.php
454share/cacti/resource/script_queries/host_cpu.xml 454share/cacti/resource/script_queries/host_cpu.xml
455share/cacti/resource/script_queries/host_disk.xml 455share/cacti/resource/script_queries/host_disk.xml
456share/cacti/resource/script_queries/unix_disk.xml 456share/cacti/resource/script_queries/unix_disk.xml
457share/cacti/resource/script_server/host_cpu.xml 457share/cacti/resource/script_server/host_cpu.xml
458share/cacti/resource/script_server/host_disk.xml 458share/cacti/resource/script_server/host_disk.xml
459share/cacti/resource/snmp_queries/host_disk.xml 459share/cacti/resource/snmp_queries/host_disk.xml
460share/cacti/resource/snmp_queries/interface.xml 460share/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
463share/cacti/resource/snmp_queries/netware_cpu.xml 463share/cacti/resource/snmp_queries/netware_cpu.xml
464share/cacti/resource/snmp_queries/netware_disk.xml 464share/cacti/resource/snmp_queries/netware_disk.xml
465share/cacti/rra.php 465share/cacti/rra.php
466share/cacti/rra/.htaccess 466share/cacti/rra/.htaccess
467share/cacti/script_server.php 467share/cacti/script_server.php
468share/cacti/script_server.pl 468share/cacti/script_server.pl
469share/cacti/scripts/3com_cable_modem.pl 469share/cacti/scripts/3com_cable_modem.pl
470share/cacti/scripts/diskfree.pl 470share/cacti/scripts/diskfree.pl
471share/cacti/scripts/diskfree.sh 471share/cacti/scripts/diskfree.sh
472share/cacti/scripts/freebsd_memory.pl 472share/cacti/scripts/freebsd_memory.pl
473share/cacti/scripts/linux_memory.pl 473share/cacti/scripts/linux_memory.pl
474share/cacti/scripts/loadavg.pl 474share/cacti/scripts/loadavg.pl
475share/cacti/scripts/loadavg_multi.pl 475share/cacti/scripts/loadavg_multi.pl
 476${PLIST.localmemory}share/cacti/scripts/local_memory.pl
476share/cacti/scripts/netbsd_memory.pl 477share/cacti/scripts/netbsd_memory.pl
477share/cacti/scripts/ping.pl 478share/cacti/scripts/ping.pl
478share/cacti/scripts/query_host_cpu.php 479share/cacti/scripts/query_host_cpu.php
479share/cacti/scripts/query_host_partitions.php 480share/cacti/scripts/query_host_partitions.php
480share/cacti/scripts/query_unix_partitions.pl 481share/cacti/scripts/query_unix_partitions.pl
481share/cacti/scripts/solaris_memory.pl 482share/cacti/scripts/solaris_memory.pl
482share/cacti/scripts/sql.php 483share/cacti/scripts/sql.php
483share/cacti/scripts/ss_fping.php 484share/cacti/scripts/ss_fping.php
484share/cacti/scripts/ss_host_cpu.php 485share/cacti/scripts/ss_host_cpu.php
485share/cacti/scripts/ss_host_disk.php 486share/cacti/scripts/ss_host_disk.php
486share/cacti/scripts/ss_sql.php 487share/cacti/scripts/ss_sql.php
487share/cacti/scripts/unix_processes.pl 488share/cacti/scripts/unix_processes.pl
488share/cacti/scripts/unix_tcp_connections.pl 489share/cacti/scripts/unix_tcp_connections.pl

File Added: pkgsrc/net/cacti/files/cacti-poller
#!@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

cvs diff -r1.1.1.1 -r1.2 pkgsrc/net/cacti/files/netbsd_memory.pl (expand / switch to unified diff)

--- pkgsrc/net/cacti/files/netbsd_memory.pl 2011/11/22 22:23:13 1.1.1.1
+++ pkgsrc/net/cacti/files/netbsd_memory.pl 2014/01/16 21:10:00 1.2
@@ -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
3if (($ARGV[0] =~ /Cached:/) || ($ARGV[0] =~ /SwapFree:/)) {  33use warnings;
4 open(PROCESS, "pstat -ks |");  34use strict;
5 $s = 0;  35
6 while (<PROCESS>) {  36use 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#
14else { # $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>) {  46sub 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.');
23close(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
 71if (scalar(@ARGV) < 1) {
 72 die('Please provide a field name');
 73}
 74
 75my $page_size = POSIX::sysconf(&POSIX::_SC_PAGESIZE);
 76my %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.
 80my $value = 0;
 81if ($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
 98print $value * ($page_size / 1024);
 99exit(0);