Example #1
0
    """docstring for MyQPSServer"""

    def __init__(self, arg):
        super(MyQPSServer, self).__init__()
        self.counter = arg

    def logic(self, d_in):
        """[summary]
        这个是我们测试QPS的“业务逻辑”,做的事情就是每传输100K数据,打印对应的时间
        [description]

        Arguments:
            d_in {[type]} -- [description]:发送的格式化数据
        """
        self.counter += 1
        # 当计数器每次达到10万,打印出响应的时间;
        if self.counter % 100000 == 0:
            print self.counter, time.time()
        return("a")


if __name__ == '__main__':

    # 初始化计数器为0
    qpsserver = MyQPSServer(0)

    # 监听在0.0.0.0:9076
    reverseD = nbNet('0.0.0.0', 60006, qpsserver.logic)
    # 状态机开始运行,除非被kill,否则永不退出
    reverseD.run()
Example #2
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from nbNet.nbNetFramework import nbNet
import subprocess

if __name__ == '__main__':
    def logic(sock_state, fd):
        print 'a'
        args = shlex.split(sock_state.buff_read)
        print 'A'
        try:
            print 'HAHA'
            sock_state.subp_obj = subprocess.Popen(args, stdout = fd, stderr = fd)
            sock_state.state = 'writeend'
            print 'haha'
        except Exception, msg:
            print msg
            os.write(fd, str(msg) + '\r\n\r\n')
            print "%r" % str(msg) + '\r\n\r\n'
            sock_state.state = "closing"
    reversdD = nbNet('0.0.0.0', 9079, logic)
    reversdD.run()
Example #3
0
# -*- coding: utf-8 -*-
# @Author: Kang.Cunhua
# @Date:   2015-12-24 10:02:46
# @Last Modified by:   Kang.Cunhua
# @Last Modified time: 2015-12-24 16:39:53
import sys
import os
import socket
import subprocess
import fcntl
import tempfile

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from nbNet.nbNetFramework import nbNet


if __name__ == '__main__':
    def logic(coming_sock_fd, d_in):
        out_temp = tempfile.SpooledTemporaryFile(bufsize=10 * 1000)
        fileno = out_temp.fileno()
        obj = subprocess.Popen(d_in, shell=True, stdout=fileno, stderr=fileno)
        obj.wait()
        out_temp.seek(0)
        callres = '%s %s %s %s' % ('==fd==', coming_sock_fd, '==fd==', out_temp.read())
        print '返回输出%010d%s' % (len(callres),  callres)
        # print '=========d_in start=========\n', d_in, '=========d_in end=========\n'
        return callres

    reverseD = nbNet('0.0.0.0', 9999, logic)
    reverseD.run()
Example #4
0
#!/usr/bin/env python
# coding=utf-8

import sys, os
import subprocess
import fcntl

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from nbNet.nbNetFramework import nbNet

if __name__ == '__main__':

    def logic(coming_sock_fd, d_in):
        #return os.popen(d_in).read()
        sub_obj = subprocess.Popen(d_in,
                                   shell=True,
                                   stdout=coming_sock_fd,
                                   stderr=coming_sock_fd)
        #out = os.popen4(d_in)[1]
        return sub_obj

    reverseD = nbNet('0.0.0.0', 9079, logic)
    reverseD.run()
Example #5
0
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from nbNet.nbNetFramework import nbNet, sendData_mh

saver_l = ["localhost:50001", "127.0.0.1:50001"]
ff_l = ["localhost:50002", "127.0.0.1:50002"]

saver_sock_l = [None]
ff_sock_l = [None]

def sendSaver(d_in, saver_l):
    return sendData_mh(saver_sock_l, saver_l, d_in)

def sendFf(d_in, ff_l):
    return sendData_mh(ff_sock_l, ff_l, d_in)

if __name__ == '__main__':
    def logic(d_in):
        #ret = sendFf(d_in, ff_l)
        ret = sendSaver(d_in, saver_l)
        print ret
        if ret:
            return("OK")
        else:
            return("ER")

    transD = nbNet('0.0.0.0', 50000, logic)
    transD.run()


Example #6
0
class MyQPSServer(object):
    """docstring for MyQPSServer"""
    def __init__(self, arg):
        super(MyQPSServer, self).__init__()
        self.counter = arg

    def logic(self, d_in):
        """[summary]
        这个是我们测试QPS的“业务逻辑”,做的事情就是每传输100K数据,打印对应的时间
        [description]

        Arguments:
            d_in {[type]} -- [description]:发送的格式化数据
        """
        self.counter += 1
        # 当计数器每次达到10万,打印出响应的时间;
        if self.counter % 100000 == 0:
            print self.counter, time.time()
        return ("a")


if __name__ == '__main__':

    # 初始化计数器为0
    qpsserver = MyQPSServer(0)

    # 监听在0.0.0.0:9076
    reverseD = nbNet('0.0.0.0', 60006, qpsserver.logic)
    # 状态机开始运行,除非被kill,否则永不退出
    reverseD.run()
Example #7
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: kang.Cunhua
# @Date:   2015-12-23 16:30:16
# @Last Modified by:   Kang.Cunhua
# @Last Modified time: 2015-12-23 16:39:03

import sys
import os
import subprocess
import fcntl

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from nbNet.nbNetFramework import nbNet

if __name__ == '__main__':

    def logic(coming_sock_fd, d_in):
        # return os.popen(d_in).read()
        subprocess.Popen(d_in,
                         shell=True,
                         stdout=coming_sock_fd,
                         stderr=coming_sock_fd)
        #out = os.popen4(d_in)[1]
        return None

    reverseD = nbNet('0.0.0.0', 9999, logic)
    reverseD.run()
def insertMonData(d_in):
    try:
        j = {}
        data = json.loads(d_in)
        print data
        dTime = int(data['Time'])
        hostIndex = monTables[fnvhash(data['Host']) % len(monTables)]
        for ud in data:
            if ud.startswith('UD_'):
                j[ud] = data[ud]
        ud_data = json.dumps(j)
        sql = "INSERT INTO `%s` (`host`,`mem_free`,`mem_usage`,`mem_total`,`load_avg`,`time`,`user_define`) VALUES('%s', '%d', '%d', '%d', '%s', '%d','%s')" % \
            (hostIndex, data['Host'], data['MemFree'], data['MemUsage'], data['MemTotal'], data['LoadAvg'], dTime,ud_data)
        c = db.execute(sql)
        ## 把UD_开头的监控项数据json插入到user_define数据表中
    except mysql.IntegrityError:
        pass
    

if __name__ == '__main__':
    def logic(d_in):
        insertMonData(d_in)
#        print d_in
        return("OK")

    saverD = nbNet('0.0.0.0', 50001, logic)
    saverD.run()


Example #9
0
def ff(d_in):
    """
    [['MemUsage', '>', 1900, '*****@*****.**'], ['LoadAvg', '>', 1.0, '*****@*****.**']]
    {"MemTotal": 15888, "MemUsage": 1804, "MemFree": 14083, "Host": "teach.works", "LoadAvg": 0.15, "Time": 1434246795}
    """
    # print conf.ff_conf
    # print d_in
    mon_data = json.loads(d_in)
    for rule in conf.ff_conf:
        monKey, operator, value, alarmRecv = rule
        monName = monKey + operator + str(value)
        eval_function = str(mon_data[monKey]) + operator + str(value)
        ff_result = eval(eval_function)
        if ff_result:
            alarmStatus[monName] = True
            print "Alarm", eval_function, alarmRecv
        else:
            if (alarmStatus.get(monName, False)):
                alarmStatus[monName] = False
                print "Recover", eval_function, alarmRecv


if __name__ == '__main__':
    def logic(d_in):
        ff(d_in)
#        print d_in
        return("OK")

    ffD = nbNet('0.0.0.0', 50002, logic)
    ffD.run()
Example #10
0
from nbNet.nbNetFramework import nbNet, sendData_mh

saver_l = ["localhost:50001", "127.0.0.1:50001"]
ff_l = ["localhost:50002", "127.0.0.1:50002"]

saver_sock_l = [None]
ff_sock_l = [None]


def sendSaver(d_in, saver_l):
    return sendData_mh(saver_sock_l, saver_l, d_in)


def sendFf(d_in, ff_l):
    return sendData_mh(ff_sock_l, ff_l, d_in)


if __name__ == '__main__':

    def logic(d_in):
        #ret = sendFf(d_in, ff_l)
        ret = sendSaver(d_in, saver_l)
        print ret
        if ret:
            return ("OK")
        else:
            return ("ER")

    transD = nbNet('0.0.0.0', 50000, logic)
    transD.run()
Example #11
0
import sys,os
import subprocess
import fcntl

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from nbNet.nbNetFramework import nbNet

if __name__ == '__main__':
    def logic(coming_sock_fd ,d_in):
        #return os.popen(d_in).read()
        #proc = subprocess.Popen([d_in], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        #print coming_sock_fd
        #outfile = open("popen-%d.out" % (coming_sock_fd),"rw")
        #print d_in
        #subprocess.Popen("ls -l",shell=True,stdout=open("popen.out","w"))
        #subprocess.Popen(d_in , shell=True, stdout=outfile, stderr=outfile)
        subprocess.Popen(d_in , shell=True, stdout=coming_sock_fd, stderr=coming_sock_fd)
        #out = os.popen4(d_in)
        #return proc.stdout
        #return outfile
        return None

    reverseD = nbNet('0.0.0.0',9010,logic)
    reverseD.run()