<commit_before># WMI Metrics
# ===
#
# Collects a variety of system metrics every 10 seconds (by default).
# Expects a "graphite" handler on the Sensu server, eg:
#
# "graphite": {
#   "type": "tcp",
#   "socket": {
#     "host": "graphite.hw-ops.com",
#     "port": 2003
#   },
#   "mutator": "only_check_output"
# }
#
# Copyright 2014 Heavy Water Operations, LLC.
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'win32ole'

module Sensu
  module Extension
    class WMIMetrics < Check
      def name
        'wmi_metrics'
      end

      def description
        'collects system metrics, using wmi and the graphite plain-text format'
      end

      def definition
        {
          type: 'metric',
          name: name,
          interval: options[:interval],
          standalone: true,
          handler: options[:handler]
        }
      end

      def post_init
        @metrics = []
        @wmi = WIN32OLE.connect('winmgmts://')
      end

      def run
        memory_metrics do
          disk_metrics do
            cpu_metrics do
              network_interface_metrics do
                yield flush_metrics, 0
              end
            end
          end
        end
      end

      private

      def options
        return @options if @options
        @options = {
          interval: 10,
          handler: 'graphite',
          add_client_prefix: true,
          path_prefix: 'WMI',
          prefix_at_start:0
        }
        if settings[:wmi_metrics].is_a?(Hash)
          @options.merge!(settings[:wmi_metrics])
        end
        @options
      end

      def flush_metrics
        metrics = @metrics.join("\n") + "\n"
        @metrics = []
        metrics
      end

      def add_metric(*args)
        value = args.pop
        path = []
        path << options[:path_prefix] if options[:prefix_at_start]
        path << settings[:client][:name] if options[:add_client_prefix]
        path << options[:path_prefix] if ! options[:prefix_at_start]
        path = (path + args).join('.')
        @metrics << [path, value, Time.now.to_i].join(' ')
      end

      def formatted_perf_data(provider, &_callback)
        full_provider = 'Win32_PerfFormattedData_' + provider
        EM.next_tick do
          result = []
          begin
            result = @wmi.ExecQuery('select * from ' + full_provider)<commit_msg>fixed typo in wmi_metrics.rb<commit_after># WMI Metrics
# ===
#
# Collects a variety of system metrics every 10 seconds (by default).
# Expects a "graphite" handler on the Sensu server, eg:
#
# "graphite": {
#   "type": "tcp",
#   "socket": {
#     "host": "graphite.hw-ops.com",
#     "port": 2003
#   },
#   "mutator": "only_check_output"
# }
#
# Copyright 2014 Heavy Water Operations, LLC.
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'win32ole'

module Sensu
  module Extension
    class WMIMetrics < Check
      def name
        'wmi_metrics'
      end

      def description
        'collects system metrics, using wmi and the graphite plain-text format'
      end

      def definition
        {
          type: 'metric',
          name: name,
          interval: options[:interval],
          standalone: true,
          handler: options[:handler]
        }
      end

      def post_init
        @metrics = []
        @wmi = WIN32OLE.connect('winmgmts://')
      end

      def run
        memory_metrics do
          disk_metrics do
            cpu_metrics do
              network_interface_metrics do
                yield flush_metrics, 0
              end
            end
          end
        end
      end

      private

      def options
        return @options if @options
        @options = {
          interval: 10,
          handler: 'graphite',
          add_client_prefix: true,
          path_prefix: 'WMI',
          prefix_at_start: 0
        }
        if settings[:wmi_metrics].is_a?(Hash)
          @options.merge!(settings[:wmi_metrics])
        end
        @options
      end

      def flush_metrics
        metrics = @metrics.join("\n") + "\n"
        @metrics = []
        metrics
      end

      def add_metric(*args)
        value = args.pop
        path = []
        path << options[:path_prefix] if options[:prefix_at_start]
        path << settings[:client][:name] if options[:add_client_prefix]
        path << options[:path_prefix] if ! options[:prefix_at_start]
        path = (path + args).join('.')
        @metrics << [path, value, Time.now.to_i].join(' ')
      end

      def formatted_perf_data(provider, &_callback)
        full_provider = 'Win32_PerfFormattedData_' + provider
        EM.next_tick do
          result = []
          begin
            result = @wmi.ExecQuery('select * from ' + full_provider)
{'fraction_non_alphanumeric': 0.10295341098169718, 'fraction_numerical': 0.008319467554076539, 'mean_word_length': 1.8455621301775147, 'pattern_counts': {'":': 12, '<': 21, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2341221', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1392, 'n_words': 375}