add master_monitor tools

This commit is contained in:
zsx 2018-01-24 16:44:38 +08:00
parent b7b09bc2a6
commit cda4511667
10 changed files with 546 additions and 0 deletions

View File

@ -0,0 +1,2 @@
include ./Makefile.in
PROG = master_monitor

View File

@ -0,0 +1,156 @@
CC = g++
CFLAGS = -c -g -W -Wall -Wcast-qual -Wcast-align \
-Wno-long-long \
-Wpointer-arith -Werror -Wshadow -O3 \
-D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS -D_USE_FAST_MACRO
###########################################################
#Check system:
# Linux, SunOS, Solaris, BSD variants, AIX, HP-UX
SYSLIB = -lpthread -lz
CHECKSYSRES = @echo "Unknow system type!";exit 1
OSNAME = $(shell uname -s)
OSTYPE = $(shell uname -m)
ifeq ($(CC),)
CC = gcc
endif
# For FreeBSD
ifeq ($(findstring FreeBSD, $(OSNAME)), FreeBSD)
ifeq ($(findstring gcc, $(CC)), gcc)
CFLAGS += -Wstrict-prototypes
endif
CFLAGS += -DFREEBSD -D_REENTRANT
RPATH = freebsd
SYSLIB = -lcrypt -lpthread -lz -L/usr/local/lib -liconv
endif
# For Darwin
ifeq ($(findstring Darwin, $(OSNAME)), Darwin)
CFLAGS += -DMACOSX -Wno-invalid-source-encoding \
-Wno-extended-offsetof
UNIXTYPE = MACOSX
RPATH = macos
SYSLIB += -liconv -rdynamic
endif
#Path for Linux
ifeq ($(findstring Linux, $(OSNAME)), Linux)
ifeq ($(findstring i686, $(OSTYPE)), i686)
RPATH = linux32
endif
ifeq ($(findstring x86_64, $(OSTYPE)), x86_64)
RPATH = linux64
endif
ifeq ($CC, "gcc")
CFLAGS += -Wstrict-prototypes
endif
CFLAGS += -DLINUX2 -D_REENTRANT
SYSLIB += -lcrypt -ldl -lz
endif
# For MINGW
ifeq ($(findstring MINGW, $(OSNAME)), MINGW)
SYSLIB = -lpthread-2 -liconv -lz
CFLAGS += -DLINUX2 -DMINGW
UNIXTYPE = LINUX
RPATH = mingw
endif
# For MSYS
ifeq ($(findstring MSYS, $(OSNAME)), MSYS)
SYSLIB = -lpthread-2 -liconv -lz -ldl
CFLAGS += -DLINUX2 -DMINGW
UNIXTYPE = LINUX
RPATH = msys
endif
#Path for SunOS
ifeq ($(findstring SunOS, $(OSNAME)), SunOS)
ifeq ($(findstring 86, $(OSTYPE)), 86)
SYSLIB += -lsocket -lnsl -lrt
endif
ifeq ($(findstring sun4u, $(OSTYPE)), sun4u)
SYSLIB += -lsocket -lnsl -lrt
endif
ifeq ($CC, "gcc")
CFLAGS += -Wstrict-prototypes
endif
CFLAGS += -DSUNOS5 -D_REENTRANT
RPATH = sunos5_x86
endif
#Path for HP-UX
ifeq ($(findstring HP-UX, $(OSNAME)), HP-UX)
ifeq ($CC, "gcc")
CFLAGS += -Wstrict-prototypes
endif
CFLAGS += -DHP_UX -DHPUX11
PLAT_NAME=hp-ux
RPATH = hpux
endif
#Find system type.
ifneq ($(SYSPATH),)
CHECKSYSRES = @echo "System is $(shell uname -sm)"
endif
###########################################################
ACL_PATH = ../../../..
MASTER_PATH = ../..
GLOBAL_PATH = ../lib_global
CFLAGS += -I. -I$(GLOBAL_PATH) -I$(GLOBAL_PATH)/include \
-I$(MASTER_PATH) \
-I$(ACL_PATH)/lib_fiber/c/include \
-I$(ACL_PATH)/lib_fiber/cpp/include \
-I$(ACL_PATH)/lib_acl/include \
-I$(ACL_PATH)/lib_acl_cpp/include \
-I$(ACL_PATH)/lib_protocol/include
LDFLAGS = -L$(GLOBAL_PATH) -lglobal \
-L$(ACL_PATH)/lib_fiber/lib -lfiber_cpp \
-L$(ACL_PATH)/lib_acl_cpp/lib -lacl_cpp \
-L$(ACL_PATH)/lib_protocol/lib -lprotocol \
-L$(ACL_PATH)/lib_acl/lib -lacl -lfiber $(SYSLIB)
COMPILE = $(CC) $(CFLAGS)
LINK = $(CC) $(OBJ) $(LDFLAGS)
###########################################################
OBJ_PATH = ./debug
$(shell mkdir -p $(OBJ_PATH))
#Project's objs
SRC = $(wildcard *.cpp) \
$(wildcard main/*.cpp) \
$(wildcard action/*.cpp)
OBJ = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(notdir $(SRC)))
$(OBJ_PATH)/%.o: %.cpp
$(COMPILE) $< -o $@
$(OBJ_PATH)/%.o: main/%.cpp
$(COMPILE) $< -o $@
$(OBJ_PATH)/%.o: action/%.cpp
$(COMPILE) $< -o $@
.PHONY = all clean
all: RM $(OBJ)
$(LINK) -o $(PROG)
@echo ""
@echo "All ok! Output:$(PROG)"
@echo ""
RM:
rm -f $(PROG)
clean cl:
rm -f $(PROG)
rm -f $(OBJ)
rebuild rb: clean all
install:
@mkdir -p ../../../../dist/master/libexec/$(RPATH)
@mkdir -p ../../../../dist/master/conf/service
cp $(PROG) ../../../../dist/master/libexec/$(RPATH)/
cp $(PROG).cf ../../../../dist/master/conf/service/
###########################################################

View File

@ -0,0 +1,27 @@
#include "stdafx.h"
#include "master_service.h"
int main(int argc, char *argv[])
{
acl::acl_cpp_init();
master_service& ms = acl::singleton2<master_service>::get_instance();
// 设置配置参数表
ms.set_cfg_int(var_conf_int_tab);
ms.set_cfg_int64(var_conf_int64_tab);
ms.set_cfg_str(var_conf_str_tab);
ms.set_cfg_bool(var_conf_bool_tab);
if (argc >= 2 && strcasecmp(argv[1], "alone") == 0)
{
const char* addr = ":8887";
acl::log::stdout_open(true);
printf("listen: %s\r\n", addr);
ms.run_alone(addr, argc >= 3 ? argv[2] : NULL);
}
else
ms.run_daemon(argc, argv);
return 0;
}

View File

@ -0,0 +1,126 @@
service master_monitor
{
# 进程是否禁止运行
master_disable = no
# 服务地址及端口号
# for master_type = inet
# master_service = 127.0.0.1:5001
# for master_type = unix
# master_service = echo.sock
# for master_type = sock
master_service = 127.0.0.1:5001, 5002, :5003, echo.sock@unix, echo2.sock@unix
# 服务监听为域套接口
# master_service = aio_echo.sock
# 服务类型
# master_type = inet
# master_type = unix
master_type = sock
# 停止子进程时是否采用强行停止的方式(即给子进程发送 SIGTERM 信号)
master_stop_kill = false
# master_stop_kill true 该配置决定是否要等待子进程退出
master_stop_wait = false
# 当系统支持 SO_REUSEPORT 是否启用该功能
master_reuseport = yes
# 是否针对监听套接口设定为非阻塞方式
master_nonblock = yes
# 当系统支持 TCP_FASTOPEN 是否启用该功能
master_fastopen = no
# 当子进程异常退出时如果该值非空则将子进程异常退出的消息通知该服务
# master_notify_addr = 127.0.0.1:5801
# 邮件通知接收者
# master_notify_recipients = zhengshuxin@hotmail.com
# 是否允许延迟接受客户端连接如果为0则表示关闭该功能如果大于0则表示打开此功能
# 并且此值代表延迟接受连接的超时值超过此值时如果客户端依然没有发来数据则操作
# 系统会在系统层直接关闭该连接
# master_defer_accept = 0
# 是否只允许私有访问, 如果为 y, 则域套接口创建在 {install_path}/var/log/private/ 目录下,
# 如果为 n, 则域套接口创建在 {install_path}/var/log/public/ 目录下,
master_private = n
master_unpriv = n
# 是否需要 chroot: n -- no, y -- yes
master_chroot = n
# 每隔多长时间触发一次单位为秒(仅对 trigger 模式有效)
master_wakeup = -
# 最大进程数
master_maxproc = 1
# 预启动进程数该值不得大于 master_maxproc
master_prefork = 1
# 进程程序名
master_command = master_monitor
# 进程日志记录文件
master_log = {install_path}/var/log/master_monitor
# 调试日志方式格式tag:level; tag:level; tab:level, all:1; 101:2
# master_debug =
# 进程启动参数只能为: -u [是否允许以某普通用户的身份运行]
# master_args =
# 传递给服务子进程的环境变量, 可以通过 getenv("SERVICE_ENV") 获得此值
# master_env = mempool_limit:512000000
# 当启动多个子进程实例时该开关控制多个子进程在接收连接时是否向 acl_master 发送消息报告自己的状态
# master_status_notify = 1
# 是否允许产生 core 文件
# fiber_enable_core = 1
# 每个进程实例处理连接数的最大次数超过此值后进程实例主动退出
fiber_use_limit = 0
# 每个进程实例的空闲超时时间超过此值后进程实例主动退出
fiber_idle_limit = 0
# 每个进程启动的线程数
fiber_threads = 1
# 进程运行时所在的路径
fiber_queue_dir = {install_path}/var
# 读写超时时间, 单位为秒
fiber_rw_timeout = 120
# 读缓冲区的缓冲区大小
fiber_buf_size = 8192
# 进程运行时的用户身份
fiber_owner = root
# 当启用 master_dispatch 连接分开服务后该配置指定 master_dispatch 所监听的
# 域套接口的全路径这样本子进程就可以从 master_dispatch 获得客户端连接
# fiber_dispatch_addr = {install_path}/var/private/dispatch.sock
# fiber_dispatch_addr 开启后下面参数控制本服务进程发给前端 master_dispatch 的服务标识信息
# fiber_dispatch_type = default
# 线程的堆栈空间大小单位为字节
fiber_stack_size = 64000
# 允许访问 udserver 的客户端IP地址范围
# fiber_access_allow = 127.0.0.1:255.255.255.255, 127.0.0.1:127.0.0.1
fiber_access_allow = all
# acl_master 退出时如果该值置1则该程序不等所有连接处理完毕便立即退出
fiber_quick_abort = 1
# fiber_quick_abort 0 且本配置项大于 0 该配置项才有效指定了
# 本进程在所有连接退出前的最大等待时间()
fiber_wait_limit = 0
############################################################################
# 应用自己的配置选项
# mysql 服务地址
# mysql_dbaddr = /tmp/mysql.sock
# mysql_dbaddr = 10.0.250.199:3306
# 连接 mysql 数据库的连接池的最大值
# mysql_dbmax = 200
# ping mysql 连接的间隔时间, 以秒为单位
# mysql_dbping = 10
# mysql 连接空闲的时间间隔, 以秒为单位
# mysql_dbtimeout = 30
# 数据库名称
# mysql_dbname = fiber_db
# 数据库访问用户
# mysql_dbuser = fiber_user
# 数据库用户访问密码
# mysql_dbpass = 111111
# 是否输出当前内存的状态信息
# debug_mem = 1
# 是否在一个线程中连接读
# loop_read = 1
}

View File

@ -0,0 +1,128 @@
#include "stdafx.h"
#include "json/service_struct.h"
#include "json/service_struct.gson.h"
#include "tools.h"
#include "guard_report.h"
#include "daemon/json/serialize.h"
#include "master_service.h"
char *var_cfg_guard_manager;
char *var_cfg_master_ctld;
acl::master_str_tbl var_conf_str_tab[] = {
{ "guard_manager", "master.qiyi.domain:8390", &var_cfg_guard_manager },
{ "master_ctld", "127.0.0.1:8290", &var_cfg_master_ctld },
{ 0, 0, 0 }
};
acl::master_bool_tbl var_conf_bool_tab[] = {
{ 0, 0, 0 }
};
static int var_cfg_io_timeout;
acl::master_int_tbl var_conf_int_tab[] = {
{ "io_timeout", 120, &var_cfg_io_timeout, 0, 0 },
{ 0, 0 , 0 , 0, 0 }
};
acl::master_int64_tbl var_conf_int64_tab[] = {
{ 0, 0 , 0 , 0, 0 }
};
//////////////////////////////////////////////////////////////////////////
master_service::master_service(void)
{
}
master_service::~master_service(void)
{
}
void master_service::handle(const acl::string& data)
{
service_dead_res_t res;
acl::url_coder coder;
coder.decode(data);
const char* ptr = coder.get("proc");
if (ptr && *ptr)
res.path = ptr;
ptr = coder.get("ver");
if (ptr && *ptr)
res.version = ptr;
ptr = coder.get("pid");
if (ptr && *ptr)
{
int pid = atoi(ptr);
res.pid = pid;
}
ptr = coder.get("rcpt");
if (ptr && *ptr)
res.rcpt = ptr;
ptr = coder.get("info");
if (ptr && *ptr)
res.info = ptr;
res.status = 200;
res.cmd = "service_dead";
acl::string body;
serialize<service_dead_res_t>(res, body);
guard_report report(var_cfg_guard_manager, 10, 10);
report.report(body);
printf("body=|%s|\r\n", body.c_str());
}
void master_service::on_accept(acl::socket_stream& conn)
{
conn.set_rw_timeout(10);
acl::string buf;
while (true)
{
if (conn.gets(buf) == false)
break;
if (!buf.empty())
handle(buf);
}
}
void master_service::proc_pre_jail(void)
{
logger(">>>proc_pre_jail<<<");
}
void master_service::proc_on_listen(acl::server_socket& ss)
{
logger(">>>listen %s ok<<<", ss.get_addr());
}
void master_service::proc_on_init(void)
{
logger(">>>proc_on_init<<<");
}
void master_service::proc_on_exit(void)
{
logger(">>>proc_on_exit<<<");
}
bool master_service::proc_on_sighup(acl::string&)
{
logger(">>>proc_on_sighup<<<");
return true;
}

View File

@ -0,0 +1,37 @@
#pragma once
extern acl::master_str_tbl var_conf_str_tab[];
extern acl::master_bool_tbl var_conf_bool_tab[];
extern acl::master_int_tbl var_conf_int_tab[];
extern acl::master_int64_tbl var_conf_int64_tab[];
//////////////////////////////////////////////////////////////////////////
class master_service : public acl::master_fiber
{
public:
master_service(void);
~master_service(void);
protected:
// @override
void on_accept(acl::socket_stream& conn);
// @override
void proc_on_listen(acl::server_socket& ss);
// @override
void proc_pre_jail(void);
// @override
void proc_on_init(void);
// @override
void proc_on_exit(void);
// @override
bool proc_on_sighup(acl::string&);
private:
void handle(const acl::string& data);
};

View File

@ -0,0 +1,8 @@
// stdafx.cpp : 只包括标准包含文件的源文件
// master_threads.pch 将成为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: 在 STDAFX.H 中
//引用任何所需的附加头文件,而不是在此文件中引用

View File

@ -0,0 +1,59 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是常用但不常更改的项目特定的包含文件
//
#pragma once
//#include <iostream>
//#include <tchar.h>
// TODO: 在此处引用程序要求的附加头文件
#include "lib_acl.h"
#include "acl_cpp/lib_acl.hpp"
#include "fiber/lib_fiber.h"
#include "fiber/lib_fiber.hpp"
#ifdef WIN32
#define snprintf _snprintf
#endif
#undef logger
#undef logger_warn
#undef logger_error
#undef logger_fatal
#undef logger_debug
#if defined(_WIN32) || defined(_WIN64)
# if _MSC_VER >= 1500
# define logger(fmt, ...) \
acl::log::msg4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# define logger_warn(fmt, ...) \
acl::log::warn4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# define logger_error(fmt, ...) \
acl::log::error4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# define logger_fatal(fmt, ...) \
acl::log::fatal4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# define logger_debug(section, level, fmt, ...) \
acl::log::msg6(section, level, __FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# else
# define logger acl::log::msg1
# define logger_warn acl::log::warn1
# define logger_error acl::log::error1
# define logger_fatal acl::log::fatal1
# define logger_debug acl::log::msg3
# endif
#else
# define logger(fmt, args...) \
acl::log::msg4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# define logger_warn(fmt, args...) \
acl::log::warn4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# define logger_error(fmt, args...) \
acl::log::error4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# define logger_fatal(fmt, args...) \
acl::log::fatal4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# define logger_debug(section, level, fmt, args...) \
acl::log::msg6(section, level, __FILE__, __LINE__, __FUNCTION__, fmt, ##args)
#endif // !_WIN32 && !_WIN64

View File

@ -0,0 +1,3 @@
#!/bin/sh
valgrind --tool=memcheck --leak-check=yes -v ./master_monitor alone master_monitor.cf