Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d50d3ee082
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 137 lines (111 sloc) 4.339 kb
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
require ::File.join(File.dirname(__FILE__), "monitorings")
module PoolParty
  class Plugins
    # MRTG plugin for poolparty
    #
    #
    # Note: This plugin does *not*
    # * control the apache config to view the /var/www/mrtg file
    # * add access control for the apache web interface
    #
    # You must add these things yourself
    #
    # Helpful Hints:
    # To test if snmp is working:
    # snmpwalk -Os -c poolpartycommunity -v 1 localhost system
    # snmpwalk -v 1 -c poolpartycommunity localhost IP-MIB::ipAdEntIfIndex
    #
    plugin :mrtg do
      include PoolParty::Mrtg::Monitorings

      def enable
        install
      end

      def monitor(*names)
        unless @installed_mrtg_monitors
          names.each {|arg| (@@monitors ||= []) << arg}
          @installed_mrtg_monitors = true
        end
        install
      end
            
      def install
       unless @installed
          has_package(:name => "mrtg", :ensures => "installed", :requires => get_package("snmpd"))
          has_package(:name => "snmpd")

          has_directory("/var/www/mrtg", :requires => get_package("mrtg"))

          prepare_monitors
          install_monitor_base_binaries
          create_configs

          has_exec(:name => "generate_mrtg_index_file",
            :command => "/usr/bin/indexmaker --output=/var/www/mrtg/index.html /etc/mrtg.cfg",
            # :ifnot => "test -f /var/www/mrtg/index.html",
            :requires => [get_directory("/var/www/mrtg"), get_file("/etc/mrtg.cfg")]
            )

          has_cron(:name => "Run MRTG",
                   :command => "/usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg.cfg --logging /var/log/mrtg.log",
                   :minute => "*/5")


          @installed = true
        end

      end

      def base_install
        unless @base_install
          has_exec({:name => "restart-snmpd", :command => "/etc/init.d/snmpd restart", :refreshonly => "true"})

          configs
          @base_install = true
        end
      end

      def prepare_monitors
        unless @prepared_mrtg_monitors
          @@monitors.each do |name|
            self.send("enable_#{name}")
          end
          @prepared_mrtg_monitors = true
        end
      end

      def create_configs
        unless @configs
          has_variable(:name => "community_name", :value => "poolpartycommunity")
          has_variable(:name => "mrtg_bin_dir", :value => "/usr/bin/mrtg_helpers")
          has_variable(:name => "internal_ip", :value => "generate('/usr/bin/curl', '-s', 'http://169.254.169.254/latest/meta-data/local-ipv4')") # or hostname -i | cut -d " " -f3-

          has_file({:name => "/etc/default/snmpd",
                    :template => File.dirname(__FILE__) + "/templates/snmpd",
                    :mode => 644})

          has_file({:name => "/etc/snmp/snmpd.conf",
                    :template => File.dirname(__FILE__) + "/templates/snmpd.conf",
                    :mode => 644})

          # concat all monitor configs
          mrtg_cnf = File.read(File.dirname(__FILE__) + "/templates/mrtg.cfg.erb")
          @@monitors.each do |name|
            cf = File.dirname(__FILE__) + "/templates/cfg/#{name}.cfg"
            (mrtg_cnf << "\n" << File.read(cf)) if File.exists?(cf)
          end

          File.open(PoolParty::Cloud::Base.storage_directory + "/templates/mrtg.cfg.erb", "w") do |f|
            f << mrtg_cnf
          end

          has_file({:name => "/etc/mrtg.cfg",
                    :content => "template('mrtg.cfg.erb')",
                    :mode => 644})

          @configs = true
        end
      end

      def install_monitor_base_binaries
        has_directory("/usr/bin/mrtg_helpers")

        Dir.glob(File.dirname(__FILE__) + "/templates/bin/*.*").each do |filename|
          helper_bin(File.basename(filename))
        end
      end

      def helper_bin(name, ensureer='present')
        has_file(:name => "/usr/bin/mrtg_helpers/#{name}") do
          template File.dirname(__FILE__) + "/templates/bin/#{name}"
          ensures ensureer
          mode 755
        end
      end

      def enable_apache_ssi # todo
        # apache_load_module("include")
        # Options +Includes
        # XBitHack on
        # chmod +x /var/www/mrtg/index.html
      end

    end
  end
end
Something went wrong with that request. Please try again.