独自のバインディングを追加することにより、collectdの機能を拡張します

Hello%habraUser%

この記事では、Pythonに新しいバインダーを追加して、収集された統計情報収集システムを拡張する方法について説明します。

この記事は、この記事を補足するものです。



問題の声明



nginxのustatsモジュールからcollectdを使用してデータを収集する必要があります。 HTTP 499、HTTP 500、HTTP 503、およびTCPエラーを収集し、同じグラフに表示する必要があります。



問題解決



収集された統計を収集する方法を学習した後、次の方法でデータを収集することが明らかになりました。



collectd自体の動作をより詳細に理解するために、Pythonで独自のプラグインを作成することにしました



プラグインのアルゴリズムは次のとおりです



データを取得します。


collectdのモジュールには

import collectd





必要なモジュールもロードします

import json, urllib2





url変数に、ustatsデータがある場所のリンクをjson形式で保存します。これは、collectd configから取得します。

url = None





ustatsでデータを取得する

 def fetch_data(url): response = urllib2.urlopen(urllib2.Request(url)) data = json.loads(response.read()) return data
      
      







構成からデータを取得します。



 def configure_callback(conf): global url for c in conf.children: if c.key == 'UstatsURL': url = c.values[0] elif c.key == 'Verbose': VERBOSE_LOGGING = bool(c.values[0]) else: collectd.warning ('ustats_info plugin: Unknown config key: %s.' % c.key) log_verbose('Configured with url=%s' % (url))
      
      







さらに、プラグインは「リーダー」になるため、データを収集するディスパッチ()関数を理解する必要があります。

ディスパッチ([type] [、values] [、plugin_instance] [、type_instance] [、plugin] [、host] [、time] [、interval])->なし。

type-データ型

-collectdに送信される実際の値。 データ型はtypes.dbで説明されています

plugin_instance-プラグインインスタンス

type_instance-型インスタンス

plugin-プラグインの名前、デフォルトではpython

interval-同じデータ型の最初と2番目の送信間の時間(秒単位)。 正の整数でなければなりません。 数値が負の場合、間隔はデフォルト値(10秒)になります



collectdにデータを送信する機能

 def dispatch_value(plugin_instance, info, key, type, type_instance=None): if not type_instance: type_instance = key value = int(info) log_verbose('Sending value: %s=%s' % (type_instance, value)) val = collectd.Values(plugin='ustats_info') val.plugin_instance = plugin_instance val.type = type val.type_instance = type_instance val.values = [value] val.dispatch()
      
      







収集されたデータは次のとおりです。

host "/" plugin ["-" plugin instance] "/" type ["-" type instance]





ここにあります:

plugin_instance =アップストリーム

タイプ =バックエンド

type_instance =エラー

出力では、次のようなものが得られます。

host / plugin-upstream / backend-tcperror.rrd



実行されるメイン関数はループで収集されました

 def read_callback(): global old_data log_verbose('Read callback called') data = fetch_data(url) last_backend = None if not data: collectd.error('ustats plugin: No data received') return if old_data == None: old_data = data upstreams = [] for key in data.keys(): upstreams.append(key) for upstream in upstreams: for index in range(len(data[upstream])): if data[upstream][index] and data[upstream][index] !=1 and data[upstream][index][0] != last_backend: dispatch_value(upstream, getValue(data[upstream][index][4], old_data[upstream][index][4]), 'http_499_errors', data[upstream][index][0].partition(":")[0]) dispatch_value(upstream, getValue(data[upstream][index][5], old_data[upstream][index][5]), 'http_500_errors', data[upstream][index][0].partition(":")[0]) dispatch_value(upstream, getValue(data[upstream][index][6], old_data[upstream][index][6]), 'http_503_errors', data[upstream][index][0].partition(":")[0]) dispatch_value(upstream, getValue(data[upstream][index][7], old_data[upstream][index][7]), 'tcp_errors', data[upstream][index][0].partition(":")[0]) dispatch_value(upstream, getValue(data[upstream][index][13], old_data[upstream][index][13]), 'total_errors', data[upstream][index][0].partition(":")[0]) last_backend = data[upstream][index][0] old_data = data
      
      







リーダー機能と設定機能を登録した後

 collectd.register_config(configure_callback) collectd.register_read(read_callback)
      
      







プラグインの準備はできていますが、収集することが知られていないタイプを使用し、my_types.dbに追加します

backend value:GAUGE:0:65535





collectd.confにプラグインを記述します

 <Plugin python > ModulePath "/usr/lib/collectd/plugins/python" #   ,    Import "ustats_info" #   <Module ustats_info> UstatsURL "http://localhost/ustats?json" Verbose true </Module> </Plugin>
      
      







おわりに



チャートはこの種のものです



GitHubのスクリプトの全文

この記事が収集された収集統計の拡大に役立つことを願って



All Articles