Skip to content

Commit

Permalink
Change python ssdb dependency to solrex/ssdb-py.
Browse files Browse the repository at this point in the history
  • Loading branch information
solrex committed Dec 5, 2017
1 parent 42cf79c commit a927232
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

SSDB Monitor Script for Open Falcon

### Step 1: Edit conf
### Step 1: Install ssdb-py

```bash
git clone https://github.com/solrex/ssdb-py.git
cd ssdb-py
sudo python ./setup.py install
```


### Step 2: Edit conf

Rename `ssdb-open-falcon.yml.default` to `ssdb-open-falcon.yml`, then edit the file, and add your ssdb servers.

Expand All @@ -17,7 +26,7 @@ ssdb-clusters:

```

### Step 2: Add the monitor script to crontab
### Step 3: Add the monitor script to crontab

```
$ crontab -l
Expand All @@ -28,7 +37,15 @@ $ crontab -l

用于 Open Falcon 的 SSDB 监控采集脚本

### 第一步:编辑配置文件
### 第一步:安装 ssdb-py

```bash
git clone https://github.com/solrex/ssdb-py.git
cd ssdb-py
sudo python ./setup.py install
```

### 第二步:编辑配置文件

`ssdb-open-falcon.yml.default` 重命名为 `ssdb-open-falcon.yml`,然后编辑这个文件,添加你要监控的 SSDB 服务器信息。

Expand All @@ -43,7 +60,7 @@ ssdb-clusters:

```

### 第二步:将监控脚本添加到 crontab 中定时执行
### 第三步:将监控脚本添加到 crontab 中定时执行

```
$ crontab -l
Expand Down
51 changes: 11 additions & 40 deletions bin/ssdbmetrics.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
#!/usr/bin/env python

import threading
# Official version of ssdb-py does not support 'info' command.
# Please use https://github.com/solrex/ssdb-py
# Install: sudo python ./setup.py install
import ssdb
import json
import time
from datetime import datetime
import requests
import re
from itertools import izip_longest

def info_parser(lst):
res = dict(izip_longest(*[iter(lst[1:])] * 2, fillvalue=None))
binlogs = re.split(r"[\W\n:]+", res['binlogs'].strip())
res['binlogs'] = dict(zip(binlogs[::2], binlogs[1::2]))
data_key_range = re.split(r" *[\n:] +", res['data_key_range'].strip())
res['data_key_range'] = dict(zip(data_key_range[::2], data_key_range[1::2]))
serv_key_range = re.split(r" *[\n:] +", res['serv_key_range'].strip())
res['serv_key_range'] = dict(zip(serv_key_range[::2], serv_key_range[1::2]))
leveldb_stats = res['leveldb.stats'].strip().split('\n')
stats_list = []
for line in leveldb_stats:
if line.find('Compactions') == -1 and line.find('Level') == -1 and line.find('------') == -1:
stats = line.strip().split()
stats_dict = dict()
stats_dict["level"] = int(stats[0])
stats_dict["files"] = int(stats[1])
# Convert MB to byte
stats_dict["size"] = int(stats[2]) * 1024 * 1024
stats_dict["time"] = int(stats[3])
stats_dict["read"] = int(stats[4]) * 1024 * 1024
stats_dict["write"] = int(stats[5]) * 1024 * 1024
if int(stats[0]) == len(stats_list):
stats_list.append(stats_dict)
res['leveldb.stats'] = stats_list
res['name'] = lst[0]
return res


class SSDBMetrics(threading.Thread):
def __init__(self, falcon_conf, ssdb_conf):
Expand All @@ -49,7 +22,7 @@ def __init__(self, falcon_conf, ssdb_conf):

self.gauge_keywords = ['links', 'dbsize']
self.counter_keywords = ['total_calls']
self.level_db_keywords = ['files', 'size']
self.level_db_keywords = ['Files', 'Size(MB)']

super(SSDBMetrics, self).__init__(None, name=self.ssdb_conf['endpoint'])
self.setDaemon(False)
Expand All @@ -67,31 +40,29 @@ def new_metric(self, metric, value, type = 'GAUGE'):

def run(self):
try:
self.ssdb = ssdb.SSDB(host = self.ssdb_conf['host'], port = self.ssdb_conf['port'])
self.ssdb.execute_command("auth", self.ssdb_conf['password'])
self.ssdb.set_response_callback('info', info_parser)
self.ssdb = ssdb.SSDB(host = self.ssdb_conf['host'], port = self.ssdb_conf['port'], password = self.ssdb_conf['password'])
falcon_metrics = []
# Statistics
self.timestamp = int(time.time())
ssdb_info = self.ssdb.execute_command("info")
ssdb_info = self.ssdb.info()
# Original metrics
for keyword in self.gauge_keywords:
falcon_metric = self.new_metric("ssdb." + keyword, int(ssdb_info[keyword]))
falcon_metrics.append(falcon_metric)
for keyword in self.counter_keywords:
falcon_metric = self.new_metric("ssdb." + keyword, int(ssdb_info[keyword]), type='COUNTER')
falcon_metrics.append(falcon_metric)
for level_stat in ssdb_info["leveldb.stats"]:
for level_stat in ssdb_info['leveldb']['stats']:
for keyword in self.level_db_keywords:
falcon_metric = self.new_metric("ssdb.level_%d_%s" % (level_stat['level'], keyword), level_stat[keyword])
falcon_metric = self.new_metric("ssdb.level_%d_%s" % (level_stat['Level'], keyword), level_stat[keyword])
falcon_metrics.append(falcon_metric)
if self.falcon_conf['test_run']:
print json.dumps(falcon_metrics)
print(json.dumps(falcon_metrics))
else:
req = requests.post(self.falcon_conf['push_url'], data=json.dumps(falcon_metrics))
print datetime.now(), "INFO: [%s]" % self.ssdb_conf['endpoint'], "[%s]" % self.falcon_conf['push_url'], req.text
print(datetime.now(), "INFO: [%s]" % self.ssdb_conf['endpoint'], "[%s]" % self.falcon_conf['push_url'], req.text)
except Exception as e:
if self.falcon_conf['test_run']:
raise
else:
print datetime.now(), "ERROR: [%s]" % self.ssdb_conf['endpoint'], e
print(datetime.now(), "ERROR: [%s]" % self.ssdb_conf['endpoint'], e)

0 comments on commit a927232

Please sign in to comment.