# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 114710 2013-12-14 04:38:05Z eborisch@macports.org $

PortSystem          1.0

name                zabbix2
version             2.2.1
revision            0
categories          net
maintainers         nomaintainer
platforms           darwin

license             GPL-2

description         An open source application and network monitor

long_description    Zabbix is the ultimate open source availability and \
                    performance monitoring solution. Zabbix offers advanced \
                    monitoring, alerting, and visualization features today \
                    which are missing in other monitoring systems, even some \
                    of the best commercial ones. 

distname            zabbix-${version}
livecheck.name      zabbix
livecheck.regex     "Stable\/(\[0-9.\]+\.\[0-9.\]+)\/"
homepage            http://www.zabbix.com/
master_sites \
    sourceforge:project/zabbix/ZABBIX%20Latest%20Stable/${version} \
    sourceforge:project/zabbix/ZABBIX%20Release%20Candidates/${version}

checksums \
    rmd160  24cfecc6825245b8a3ae15c23ffe38236cd37cd4 \
    sha256  29c9e4cefb527bf929797b26a5a1d8a07c55675bfe6a86ff9ef8e46bd792c7fb

universal_variant   no

depends_lib         port:OpenIPMI

conflicts           zabbix

dist_subdir         zabbix2

subport zabbix2-agent {}

configure.args      --bindir=${prefix}/bin/zabbix \
                    --sbindir=${prefix}/sbin/zabbix \
                    --libexecdir=${prefix}/libexec/zabbix \
                    --datadir=${prefix}/share/zabbix \
                    --sysconfdir=${prefix}/etc/zabbix \
                    --localstatedir=${prefix}/var/zabbix

configure.args-append       --with-openipmi=${prefix}

configure.ldflags-append    -lresolv

startupitem.create      yes

if { ${name} ne ${subport} } {
    startupitem.name        zabbix2-agentd
    startupitem.executable  \
        ${prefix}/sbin/zabbix/zabbix_agentd \
        -c ${prefix}/etc/zabbix/zabbix_agentd.conf
    startupitem.pidfile     clean /tmp/zabbix_agentd.pid

    configure.args-append   --enable-agent
} else {
    depends_lib-append      port:fping \
                            port:curl \
                            port:apache2 \
                            port:php5 \
                            port:php5-gd \
                            port:php5-gettext \
                            port:php5-mbstring \
                            port:php5-sockets

    configure.args-append   --enable-server \
                            --with-libcurl=${prefix}/bin/curl-config \
                            --with-net-snmp

    startupitem.name        zabbix2-server
    startupitem.start       "${prefix}/share/zabbix/zabbix_server.init start"
    startupitem.stop        "${prefix}/share/zabbix/zabbix_server.init stop"

    variant full_server description {
        Adds dependencies a complete server are installed.
    } {
    }
    
    # Items are "display name" "port name" "config arg" "php interface"
    array set DBLIST {
        mysql5  {"MySQL 5.x"            mysql5 \
                 mysql=${prefix}/lib/mysql5/bin/mysql_config mysql}
        mysql51 {"MySQL 5.1"            mysql51 \
                 mysql=${prefix}/lib/mysql51/bin/mysql_config mysql}
        mysql55 {"MySQL 5.5"            mysql55 \
                 mysql=${prefix}/lib/mysql55/bin/mysql_config mysql}
        mysql56 {"MySQL 5.6"            mysql56 \
                 mysql=${prefix}/lib/mysql56/bin/mysql_config mysql}
        mariadb {"MariaDB"              mariadb \
                 mysql=${prefix}/lib/mariadb/bin/mysql_config mysql}
        percona {"Percona"              percona \
                 mysql=${prefix}/lib/percona/bin/mysql_config mysql}
        pgsql81 {"PostgreSQL 8.1.x"     postgresql81 \
                 pgsql=${prefix}/lib/postgresql81/bin/pg_config postgresql}
        pgsql82 {"PostgreSQL 8.2.x"     postgresql82 \
                 pgsql=${prefix}/lib/postgresql82/bin/pg_config postgresql}
        pgsql83 {"PostgreSQL 8.3.x"     postgresql83 \
                 pgsql=${prefix}/lib/postgresql83/bin/pg_config postgresql}
        pgsql84 {"PostgreSQL 8.4.x"     postgresql84 \
                 pgsql=${prefix}/lib/postgresql84/bin/pg_config postgresql}
        sqlite3 {"SQLite 3.x"           sqlite3 \
                 sqlite3=${prefix} sqlite}
    }
    set DBLIST_KEYS [lsort [array names DBLIST]]
    foreach dbitem $DBLIST_KEYS {
        set this_key [lsearch -exact $DBLIST_KEYS $dbitem]
        set conf_list [lreplace $DBLIST_KEYS $this_key $this_key]
        set prms $DBLIST($dbitem)
        
        set variant_line {variant $dbitem description \
          "Use [lindex ${prms} 0] for server database" }

        foreach over ${conf_list} {
            append variant_line " conflicts ${over}"
        }

        append variant_line { "
            set ::ZDB               [lindex ${prms} 1]
            set ::MYSQL_MODE        [string equal [lindex ${prms} 3] mysql]
            depends_lib-append      port:[lindex ${prms} 1]
            configure.args-append   --with-[lindex ${prms} 2]
            depends_run-append \
                port:php5-[lindex ${prms} 3]

            if {[string compare ${dbitem} sqlite3] && \
                 [variant_isset full_server]} {
                depends_run-append \
                    port:[lindex ${prms} 1]-server
            }
        " }

        eval $variant_line
    }

    if {![variant_isset mysql51] &&
        ![variant_isset mysql55] &&
        ![variant_isset mysql56] &&
        ![variant_isset mariadb] &&
        ![variant_isset percona] &&
        ![variant_isset pgsql81] &&
        ![variant_isset pgsql82] &&
        ![variant_isset pgsql83] &&
        ![variant_isset pgsql84] &&
        ![variant_isset sqlite3]} {
        default_variants +mysql5
    }

# Notify anyone with +agent_only installed of new zabbix2-agent port
    variant agent_only description {Old variant -- install zabbix2-agent} {
        pre-fetch {
            ui_error \
                "Variant 'zabbix2 +agent_only' replaced by port zabbix2-agent"
            return -code error "+agent_only variant replaced by zabbix2-agent"
        }
    }

    variant date_slash description {
        Draw date/month rather than date.month on line graphs.
    } {}
}

post-extract {
    if { ${name} == ${subport} } {
        if { ${MYSQL_MODE} == 1 &&
             [variant_isset full_server] } {
                set repstr "s|# DBSocket=/tmp/mysql.sock|"
                append repstr "DBSocket=${prefix}/var/run/${ZDB}/mysqld.sock|"
            reinplace ${repstr} ${worksrcpath}/conf/zabbix_server.conf
        }

        if {[variant_isset date_slash]} {
            reinplace -E {s|[[:<:]]d\.m[[:>:]]|d/m|g} \
                frontends/php/include/classes/graphdraw/CLineGraphDraw.php
        }
    }
    set repstr "s|# FpingLocation=/usr/sbin/fping|"
    append repstr "FpingLocation=${prefix}/sbin/fping|"
    reinplace ${repstr} ${worksrcpath}/conf/zabbix_server.conf
}

add_users zabbix group=zabbix

post-destroot {
    if { ${name} ne ${subport} } {
     ####### AGENT #######
# Copy sample agent .conf files
        xinstall -d -m 755 -d ${destroot}${prefix}/etc/zabbix
        xinstall -m 755  ${worksrcpath}/conf/zabbix_agent.conf \
            ${destroot}${prefix}/etc/zabbix/zabbix_agent.conf.sample
        xinstall -m 755  ${worksrcpath}/conf/zabbix_agentd.conf \
            ${destroot}${prefix}/etc/zabbix/zabbix_agentd.conf.sample

# Don't overwrite user settings on each install
        delete ${destroot}${prefix}/etc/zabbix/zabbix_agentd.conf
        delete ${destroot}${prefix}/etc/zabbix/zabbix_agent.conf
    } else {
     ####### SERVER #######
# Copy sample server and agent .conf files
        xinstall -d -m 755 -d ${destroot}${prefix}/etc/zabbix
        xinstall -m 755 ${worksrcpath}/conf/zabbix_server.conf \
            ${destroot}${prefix}/etc/zabbix/zabbix_server.conf.sample

# Don't overwrite user settings on each install
        delete ${destroot}${prefix}/etc/zabbix/zabbix_server.conf

# Copy database data and schemas
        xinstall -d -m 755 -d ${destroot}${prefix}/share/zabbix/data
        xinstall -d -m 755 -d ${destroot}${prefix}/share/zabbix/schema

        eval xinstall -m 755 ${worksrcpath}/database/mysql/data.sql \
            ${destroot}${prefix}/share/zabbix/data
        eval xinstall -m 755 ${worksrcpath}/database/mysql/images.sql \
            ${destroot}${prefix}/share/zabbix/data
        eval xinstall -m 755 ${worksrcpath}/database/mysql/schema.sql \
            ${destroot}${prefix}/share/zabbix/schema

        file copy ${worksrcpath}/upgrades \
            ${destroot}${prefix}/share/zabbix/

# Copy the front end files
        file mkdir ${destroot}${prefix}/share/zabbix/frontends/
        file copy ${worksrcpath}/frontends/php/ \
            ${destroot}${prefix}/share/zabbix/frontends/

# Create a startup script for the server
# This script has a delay built into it (waiting for DB to start) so we
# don't use a simple startupitem.executable approach
        if { ${MYSQL_MODE} == 1 } {
            xinstall -m 755 ${portpath}/${filesdir}/zabbix_server.init.mysql \
                ${destroot}${prefix}/share/zabbix/zabbix_server.init
            reinplace "s|__VER__|${ZDB}|g" \
                ${destroot}${prefix}/share/zabbix/zabbix_server.init
        } else {
            xinstall -m 755 ${portpath}/${filesdir}/zabbix_server.init \
                ${destroot}${prefix}/share/zabbix
        }
        reinplace "s|__PREFIX__|${prefix}|g" \
            ${destroot}${prefix}/share/zabbix/zabbix_server.init

# Set permissions for etc (protect passwords) and the frontend
        system "chmod 660 ${destroot}${prefix}/etc/zabbix/*"
        system "chown zabbix:zabbix ${destroot}${prefix}/etc/zabbix/*"
        system "chown -R www:www ${destroot}${prefix}/share/zabbix/frontends/*"

        xinstall -d -m 755 -d \
            ${destroot}${prefix}/share/zabbix/zabbix_agent_win32
        xinstall -m 755 ${worksrcpath}/bin/win32/zabbix_agentd.exe \
            ${destroot}${prefix}/share/zabbix/zabbix_agent_win32
    }
}

if { ${name} ne ${subport} } {
    notes "
####                                        ####
#### ZABBIX2 agent installation section     ####

1) Edit the sample .conf file ${prefix}/etc/zabbix/zabbix_agentd.conf.sample
   (rename & omit .sample)

   Set the following variable to the ip address of your ZABBIX server to\
   allow it access to the agent.

    Server=x.x.x.x


2) Set zabbix_agentd to run at system boot

     sudo port load zabbix2-agent

3) A Win32 agent is in ${prefix}/share/zabbix/zabbix_agent_win32 for\
   installation on Windows NT 4.0, Windows 2000, and Windows XP.

#### End ZABBIX2 agent installation section ####
####                                        ####
"
} else {
    notes "
####                                                 ####
#### Begin ZABBIX2 local server installation section ####

 (Installing with +full_server will add all of the dependants; configuration
  will still be required.)

1) Setup MySQL, i.e. via MacPorts: (for local MySQL server)

    sudo port install mysql5-server


2) Setup the ZABBIX MySQL database

 For other databases, follow documentation from 
 https://www.zabbix.com/documentation/2.0/manual/appendix/install/db_scripts

-Create the ZABBIX database
    mysql5 -u root -p (enter password at prompt)
    mysql> create database zabbix character set utf8;
    mysql> quit;

-Import the ZABBIX Schema
    cd ${prefix}/share/zabbix/schema
    cat mysql.sql | mysql5 -u root -p zabbix
    cd ${prefix}/share/zabbix/data
    cat images.sql | mysql5 -u root -p zabbix
    cat data.sql | mysql5 -u root -p zabbix

-Create a MySQL 'zabbix' user and password
    mysql5 -u root -p
    mysql> grant all privileges on zabbix.* to zabbix@localhost identified by '<zabbix-mysql-password>';
    mysql> grant all privileges on zabbix.* to zabbix identified by '<zabbix-mysql-password>';
    mysql> quit;


3) Edit the sample .conf file ${prefix}/etc/zabbix/zabbix_server.conf
   (rename & omit .sample)

   Modify these variables at the very least:

    DBName=zabbix
    DBUser=zabbix
    DBPassword=<zabbix-mysql-password>
    DBSocket=<location for your active mysql5\[15\] server socket>
      Typically ${prefix}/var/run/mysql\[51|55\]/mysqld.sock


4) Set a symbolic link in your Apache document root pointing to the PHP
   frontend files

    sudo ln -s ${prefix}/share/zabbix/frontends/php <Apache-docroot>/zabbix


5) Set zabbix_server to run at system boot, then start it

    sudo port load zabbix2

   To start Zabbix manually, use: 
    ${prefix}/share/zabbix/zabbix_server.init start (stop|status)

 !! Note: Your shared memory will likely need to be increased  !!
 !!       (see http://support.apple.com/kb/HT4022)             !!


6) A Win32 agent is in ${prefix}/share/zabbix/zabbix_agent_win32 for\
   installation on Windows NT 4.0, Windows 2000, and Windows XP.  See\
   the ReadMe.txt for instructions.


7) Open http://localhost/zabbix/ in your browser and walk through the setup,
   then login with default user 'Admin' and password 'zabbix'.


8) Read the fine manual at http://www.zabbix.com/documentation/


#### End ZABBIX2 local server installation section   ####
####                                                 ####


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! ZABBIX -> ZABBIX2 Server Upgrade process !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!  0) BACK UP YOUR DATABASE                                                !!
!!                                                                          !!
!!  1) Be sure to use 'sudo port unload zabbix' to stop the background      !!
!!     server before deactivating zabbix, and then use                      !!
!!     'sudo port load zabbix2' **AFTER UPGRADING THE DATABASE** as         !!
!!     described below.                                                     !!
!!                                                                          !!
!!  2) For upgrading from 1.8 (zabbix -> zabbix2), upgrade scripts are      !!
!!     located in ${prefix}/share/zabbix/upgrades                          !!
!!                                                                          !!
!!  For upgrade procedure, please see                                       !!
!!    http://www.zabbix.com/documentation/2.0/manual/installation/upgrade   !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
"
}
