- 
                Notifications
    You must be signed in to change notification settings 
- Fork 31
add dataStore module.database is influxDB #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            jiutianmuzi
  wants to merge
  25
  commits into
  linuxep:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
jiutianmuzi:master
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            25 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      121a5e3
              
                add dataStore module.database is influxDB
              
              
                 8bd6e48
              
                create .gitignore
              
              
                 d7b6c66
              
                delete a file test
              
              
                jiutianmuzi 46f9751
              
                Delete vcs.xml
              
              
                jiutianmuzi 3cea2c9
              
                修改类名
              
              
                 46981d5
              
                解析数据
              
              
                jiutianmuzi dff4b82
              
                add files
              
              
                jiutianmuzi 9a887c0
              
                请求lepd,存入influxdb
              
              
                jiutianmuzi bfb8244
              
                修改 write_points()
              
              
                jiutianmuzi 56f48da
              
                完成请求和存入
              
              
                jiutianmuzi 76c83f2
              
                每一个命令的请求完成
              
              
                jiutianmuzi 819cb13
              
                add run.py //同时请求
              
              
                jiutianmuzi b461495
              
                调整目录  //同时请求
              
              
                jiutianmuzi cca0030
              
                完成存储
              
              
                jiutianmuzi d2892b9
              
                完成存储
              
              
                jiutianmuzi 8906013
              
                完成存储
              
              
                jiutianmuzi f895943
              
                 修改了不断请求的bug
              
              
                jiutianmuzi 4c5fc1c
              
                 修改了不断请求因数据变化的bug
              
              
                jiutianmuzi a2477e6
              
                同时请求,保存CQ
              
              
                jiutianmuzi 4bb4a67
              
                完成解析存储
              
              
                jiutianmuzi f69db83
              
                完成解析存储
              
              
                jiutianmuzi 4c559c4
              
                完成解析存储
              
              
                jiutianmuzi 6fac701
              
                完成解析存储
              
              
                jiutianmuzi 6ba206e
              
                完成解析存储
              
              
                jiutianmuzi d6f8bef
              
                调整目录结构,全部请求
              
              
                jiutianmuzi File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #IDE | ||
| .idea/ | 
This file was deleted.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -8,7 +8,7 @@ | |
| import pprint | ||
| import re | ||
| import datetime | ||
| import click | ||
| import clock | ||
|  | ||
| class LepDClient: | ||
|  | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import argparse | ||
|  | ||
| from influxdb import InfluxDBClient | ||
|  | ||
| def main(host='localhost',port=8086): | ||
| user = 'root' | ||
| password = 'root' | ||
| dbname = 'example' | ||
| dbuser = 'smly' | ||
| dbuser_password = 'my_secret_password' | ||
| query = 'select value from cpu_load_short;' | ||
| json_body = [{ | ||
| "measurement": "cpu_load_short", | ||
| "tags":{ | ||
| "host":"server01", | ||
| "region":"us-west" | ||
| }, | ||
| "time":"2009-11-10T23:00:00Z", | ||
| "fields":{ | ||
| "value":0.64 | ||
| } | ||
| }] | ||
|  | ||
|  | ||
| client = InfluxDBClient(host,port,user,password,dbname) | ||
| client.create_database(dbname) | ||
| client.create_retention_policy('awesome_policy','3d',3,default=True) | ||
| client.switch_user(dbuser, dbuser_password) | ||
| client.write_points(json_body) | ||
| result = client.query(query) | ||
| print(result) | ||
| client.switch_user(user,password) | ||
| #client.drop_database(dbname) | ||
| def parse_args(): | ||
| parser = argparse.ArgumentParser(description='exmple code to play with InfluxDB') | ||
| parser.add_argument('--host',type=str,required = False,default='localhost', | ||
| help='hostnameof InfluxDB http Api') | ||
| parser.add_argument('--port',type=int ,required=False,default=8086, | ||
| help='port of InfluxDb http Api') | ||
| return parser.parse_args() | ||
|  | ||
| # if __name__ == '__main__': | ||
| # args = parse_args() | ||
| # main(host=args.host,port=args.port) | 
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """Core module for interacting with influxDB""" | ||
|  | ||
| __author__ = "<programmerli@foxmail.com>" | ||
| __copyright__ = "Licensed under GPLv2 or later." | ||
|  | ||
| from influxdb import InfluxDBClient | ||
|  | ||
| ''' | ||
| MyInfluxDbClient is the warpper of InfluxDBClient, | ||
| To insert data and to query data can use it | ||
| ''' | ||
|  | ||
|  | ||
| class MyInfluxDbClient: | ||
| def __init__(self, influxDBAddress,port=8086,username='root',password='',database="lep"): | ||
| self._client = InfluxDBClient(influxDBAddress, port,username,password,database) | ||
|  | ||
| def write_points(self, json_body): | ||
|  | ||
| if self._client.write_points(json_body): | ||
| return True | ||
| else: | ||
| return False | ||
|  | ||
| def query(self, statement): | ||
| return self._client.query(statement) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| """Core module for interacting with LEPD""" | ||
|  | ||
| __author__ = "Copyright (c) 2016, 李旭升 <programmerli@foxmail.com>" | ||
| __copyright__ = "Licensed under GPLv2 or later." | ||
|  | ||
| import json | ||
| import pprint | ||
| import socket | ||
|  | ||
| ''' | ||
| LepdClient pulls data from lepd | ||
| ''' | ||
|  | ||
|  | ||
| class LepdClient(object): | ||
| def __init__(self, server, port=12307, config='release'): | ||
| self.server = server | ||
| self.port = port | ||
| self.bufferSize = 2048 | ||
| self.config = config | ||
|  | ||
| self.LEPDENDINGSTRING = 'lepdendstring' | ||
|  | ||
| def sendRequest(self, methodName): | ||
| sock = None | ||
| try: | ||
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| sock.connect((self.server, self.port)) | ||
|  | ||
| input_data = {} | ||
| input_data['method'] = methodName | ||
|  | ||
| dumped_data = json.dumps(input_data) | ||
|  | ||
| sock.send(dumped_data.encode()) | ||
| serverResponse = str.encode("") | ||
| end = str.encode("lepdendstring") | ||
| while True: | ||
| data = sock.recv(self.bufferSize) | ||
| if end in data: | ||
| data = data.replace(end, str.encode("")) | ||
| serverResponse = serverResponse + data | ||
| break | ||
| serverResponse = serverResponse + data | ||
| responseJsonDecoded = json.loads(serverResponse.decode()) | ||
| return responseJsonDecoded | ||
| except Exception as error: | ||
| print("dataStore/lepdClient/LepdClient.py.sendRequest() throws an exception\n") | ||
| pass | ||
| finally: | ||
| if (sock): | ||
| sock.close() | ||
|  | ||
| def listAllMethods(self): | ||
| response = self.sendRequest('ListAllMethod') | ||
| if (response == None or 'result' not in response): | ||
| return [] | ||
| lines = response['result'].strip().split() | ||
| return lines | ||
|  | ||
|  | ||
| if (__name__ == '__main__'): | ||
| pp = pprint.PrettyPrinter(indent=2) | ||
| client = LepdClient('www.rmlink.cn', config='debug') | ||
|  | ||
| print(client.listAllMethods()) | ||
              Empty file.
          
    
              Empty file.
          
    
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| __author__ = "<programmerli@foxmail.com>" | ||
| __copyright__ = "Licensed under GPLv2 or later." | ||
|  | ||
| from dataStore.lepdClient.LepdClient import LepdClient | ||
| from dataStore.influxDbUtil.dbUtil import MyInfluxDbClient | ||
|  | ||
| import time | ||
| import re | ||
|  | ||
| ''' | ||
| fetch data related to GetCmdMpstat from lepd by lepdClient and | ||
| store the returned data into the influxDB by influxDBClient. | ||
| ''' | ||
| def pullAndStoreGetCmdMpstat(lepdClient, influxDbClient): | ||
| res = lepdClient.sendRequest('GetCmdMpstat') | ||
| # print(res) | ||
| myStr = res['result'].split('\n') | ||
|  | ||
| data = re.findall(r"\d+\.?\d*", myStr[10]) | ||
|  | ||
|  | ||
| json_body = [ | ||
| { | ||
| "measurement": "GetCmdMpstat", | ||
| "tags": { | ||
| # the address of lepd | ||
| "server": lepdClient.server | ||
| }, | ||
| # "time": "2017-03-12T22:00:00Z", | ||
| "fields": { | ||
| "%usr": float(data[0]), | ||
| "%nice": float(data[1]), | ||
| "%sys": float(data[2]), | ||
| "%iowait": float(data[3]), | ||
| "%irq": float(data[4]), | ||
| "%soft": float(data[5]), | ||
| "%steal": float(data[6]), | ||
| "%guest": float(data[7]), | ||
| "%gnice": float(data[8]), | ||
| "%idle": float(data[9]) | ||
| } | ||
|  | ||
| } | ||
| ] | ||
|  | ||
| influxDbClient.write_points(json_body) | ||
|  | ||
|  | ||
|  | ||
| if (__name__ == '__main__'): | ||
| lepdClient = LepdClient('localhost') | ||
| influxDbClient = MyInfluxDbClient('localhost') | ||
| for i in range(30): | ||
| pullAndStoreGetCmdMpstat(lepdClient, influxDbClient) | ||
| time.sleep(1) | ||
|  | ||
|  | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| CREATE CONTINUOUS QUERY cp_1_week_GetCmdIotop ON lep BEGIN SELECT mean("Total_DISK_READ") AS "meanTDR" ,mean("Total_DISK_WRITE") | ||
| AS "meanTDW" INTO lep."1_week".GetCmdIotop FROM lep."12_hours".GetCmdIotop GROUP BY time(60s) END | ||
|  | ||
|  | ||
|  | ||
|  | ||
| CREATE CONTINUOUS QUERY cp_1_week_GetCmdMpstat ON lep BEGIN | ||
| SELECT | ||
| mean("%usr") AS meanusr,mean("%nice") AS meannice, | ||
| mean("%sys") AS meansys,mean("%iowait") AS meaniowait, | ||
| mean("%irq") AS meanirq,mean("%soft") AS meansoft, | ||
| mean("%steal")AS meansteal,mean("%guest") AS meanguest, | ||
| mean("%gnice") AS meangnice,mean("%idle") AS meanidle | ||
| INTO lep."1_week".GetCmdMpstat FROM lep."12_hours".GetCmdMpstat | ||
| GROUP BY time(60s) END | ||
|  | ||
|  | ||
| CREATE CONTINUOUS QUERY cp_1_month_GetCmdMpstat ON lep BEGIN | ||
| SELECT | ||
| mean("%usr") AS meanusr,mean("%nice") AS meannice, | ||
| mean("%sys") AS meansys,mean("%iowait") AS meaniowait, | ||
| mean("%irq") AS meanirq,mean("%soft") AS meansoft, | ||
| mean("%steal")AS meansteal,mean("%guest") AS meanguest, | ||
| mean("%gnice") AS meangnice,mean("%idle") AS meanidle | ||
| INTO lep."1_month".GetCmdMpstat FROM lep."12_hours".GetCmdMpstat | ||
| GROUP BY time(600s) END | ||
|  | ||
|  | ||
| CREATE CONTINUOUS QUERY cp_1_year_GetCmdMpstat ON lep BEGIN | ||
| SELECT | ||
| mean("%usr") AS meanusr,mean("%nice") AS meannice, | ||
| mean("%sys") AS meansys,mean("%iowait") AS meaniowait, | ||
| mean("%irq") AS meanirq,mean("%soft") AS meansoft, | ||
| mean("%steal")AS meansteal,mean("%guest") AS meanguest, | ||
| mean("%gnice") AS meangnice,mean("%idle") AS meanidle | ||
| INTO lep."1_year".GetCmdMpstat FROM lep."12_hours".GetCmdMpstat | ||
| GROUP BY time(1800s) END | ||
|  | ||
|  | ||
|  | ||
| CREATE CONTINUOUS QUERY cp_1_week_GetCmdIostat ON lep BEGIN SELECT mean("rrqm/s") AS meanrrqm,mean("wrqm/s") AS meanwrqm, mean("r/s") AS meanr, mean("w/s") AS meanw, mean("rkB/s") AS meanrkB,mean("wkB/s") AS meanwkb,mean("avgrq-sz") AS meanavgrq, | ||
| mean("avgqu-sz") AS meanavgqu,mean("await") AS meanawait,mean("r_await") AS meanr_await,mean("w_await") AS meanw_await,mean("svctm") AS meansvctm,mean("%util") As meanutil INTO lep."1_week".GetCmdIostat FROM lep."12_hours".GetCmdIostat GROUP BY time(60s) END | ||
|  | ||
|  | 
              Empty file.
          
    
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why another LepdClient? we already have one, we should use that.