分类目录归档:Linux开发

Linux开发,只要在Linux环境下的一切相关开发,包括c++\goLang\Web,shell命令等。

openresty的lua简单开发

1.nginx的location的配置

location /flow1 {
        lua_need_request_body on;
        lua_code_cache on;
        default_type application/json;
        #在content阶段,lua中可以调用所有ngx_lua的函数,但不是很适合upstream这种情形。
        content_by_lua_file /usr/local/openresty/nginx/conf/vhosts/flowservice.lua;
 }
 location /flow2 {
        error_log /data/log/nginx/flow_error.log;
        access_log /data/log/nginx/cntr.log cntr;

        lua_need_request_body on;
        lua_code_cache on;
        default_type application/json;
        set $proxyhost "wps_cntr";
        rewrite_by_lua_file  /usr/local/openresty/nginx/conf/vhosts/flowservice.lua; #Rewrite阶段是不允许带参数的。
        #return 200, $proxyhost; #当使用return时,是会导致rewrite的lua不会被调用。可能是因为return函数的关系,被系统优先调用而忽略了lua文件的调用。
        proxy_pass http://$proxyhost; #默认情况下是调用wps_cntr的,由于flowserice的影响,会调用ngx.var.proxyhost="wps_flow"的。
 }
 location /flow3 {
        lua_need_request_body on;
        lua_code_cache on;
        default_type application/json;
        set $cntr_old "wps_cntr";
        set $cntr_new "wps_flow";
        # set阶段,不能使用ngx.req.read_body的函数,这个是ngx_lua的强制需求。
        set_by_lua_file $proxyhost /usr/local/openresty/nginx/conf/vhosts/flowservice.lua $cntr_old $cntr_new; #这情况是允许带参数的。
        return 200 $proxyhost
 }

2.相应flowservice.lua的脚本编写

local json_module = os.getenv("JSON_MODULE") or "cjson"
local json = require(json_module)
local g_plugin = {}

g_plugin["Kaiwpp"] = true
g_plugin["onekeyanimation"] = true

local function find_func(mod, funcnames)
    for _, v in ipairs(funcnames) do
        if mod[v] then
            return mod[v]
        end
    end
    return nil
end

local json_encode = find_func(json, { "encode", "Encode", "to_string", "stringify", "json" })
local json_decode = find_func(json, { "decode", "Decode", "to_value", "parse" })

local function get_query_string()
    local method = ngx.var.request_method
    if "GET" == method then
       local args = ngx.req.get_uri_args()
       return args['query']
    elseif "POST" == method then
       ngx.req.read_body()
       local headers = ngx.req.get_headers()
       if headers['content-type'] == 'application/json' then
          local data = ngx.req.get_body_data()
          if data == nil then
             return nil
          end
          local args = json_decode(data)
          if args then
             return args['query']
          end
          return nil
       end
       local args = ngx.req.get_post_args()
       return args['query']
    end
end
local q = get_query_string()
if q then
   local query = json_decode(q)
   if query then
      local pn = query[1]
      if pn then
         local name = pn['plug_name']
         if name then
            if g_plugin[name] then
               ngx.var.proxyhost="wps_flow"
            end
         end
      end
   end
end
ngx.log(ngx.INFO, "abc")

3.检验结果的方式。
A.lua脚本运行出错,看error.log可以看出出错原因。
B.ngx.log来打印,ngx.log默认是打印至err_log的地方。
C.lua运行正常,可以通过access_log来看。特别是upstream的模式,access_log的格式需要包括$upstream_addr的参数。
config配置

error_log

access_log

ab压测post数据

1.安装ab工具,需要依赖
yum -y install httpd-tools
2.AB命令行

#!/bin/sh
#POST方式,300个用户,100000个请求量。
ab -c 300 -n 100000 -p ./data.4 -T application/json http://127.0.0.1:7001/flow 
#PUT方式
ab -c 200 -n 100000 -u ./data.1 -T application/json http://127.0.0.1:7001/flow 

3.data.4数据内容,json格式

{"query":"[{\"app_name\":\"abcdddd\",\"status\":\"aaaaaa\"}]"}

linux内存清理

清理前

清理后

清理脚本

#! /bin/sh
used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`
echo "===========================" >> /data/logs/mem.log
date >> /data/logs/mem.log
echo "Memory usage before | [Use:${used}MB][Free:${free}MB]" >> /data/logs/mem.log
if [ $free -le 4000 ] ; then
                sync && echo 1 > /proc/sys/vm/drop_caches
                sync && echo 2 > /proc/sys/vm/drop_caches
                sync && echo 3 > /proc/sys/vm/drop_caches
                used_ok=`free -m | awk 'NR==2' | awk '{print $3}'`
                free_ok=`free -m | awk 'NR==2' | awk '{print $4}'`
                echo "Memory usage after | [Use:${used_ok}MB][Free:${free_ok}MB]" >> /data/logs/mem.log
                echo "OK" >> /data/logs/mem.log
else
                echo "Not required" >> /data/logs/mem.log
fi
exit 1

安装纯msys的开发环境

最简单的方法是直接登录:https://www.msys2.org/网址,下载安装包。
————————————————————————————————
通常使用mingw来进行跨平台移植,如果源代码只依赖到linux核心的话,可以只需要安装msys核心组件即可。
例如lrzsz组件,可以只依赖msys的相关工具,即可。
1.安装组件管理工具,【只需要安装mingw-w32-install即其管理工具在如下目录】


2.选择msys的组件

ansible的安装与运行

1.采用EPEL-release源安装,简单安全。

yum install epel-release -y
yum install ansible –y

2.验证

 
ansible 127.0.0.1 -m ping
---------------------
返回结果
127.0.0.1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

example:
https://github.com/leucos/ansible-tuto

利用systemd作守护进程的脚本

#!/bin/sh
 
path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)
mode=$1
name=receipt
systemd_dir=/usr/lib/systemd/system
app_process=`ps -ef | grep "$program"| grep -v grep`
force=$2

case "$mode" in
   'install')
        if [ -d $systemd_dir ];then
           if [ -f $systemd_dir/${name}.service ] && [ "$force" != "--force" ]; then
              echo "The ${name}.service had exist.!!!"
              echo "Force to install by command: install --force"
              exit
           fi
           echo "Description=$name service" > $systemd_dir/${name}.service
           echo "After=network.target" >> $systemd_dir/${name}.service
           echo "" >> $systemd_dir/${name}.service
           echo "[Service]" >> $systemd_dir/${name}.service
           echo "ExecStart=$path_script/$name --config=$path_script/config/config.toml" >> $systemd_dir/${name}.service
           echo "KillMode=process" >> $systemd_dir/${name}.service
           echo "Restart=on-failure" >> $systemd_dir/${name}.service
           echo "RestartSec=3s" >> $systemd_dir/${name}.service
           echo "" >> $systemd_dir/${name}.service
           echo "[Install]" >> $systemd_dir/${name}.service
           echo "WantedBy=multi-user.target" >> $systemd_dir/${name}.service
           systemctl daemon-reload
           systemctl enable $name
           systemctl start $name
        else
           echo "it's not support systemd feature"
        fi
        ;;
   'uninstall')
        if [ -f $systemd_dir/${name}.service ]; then
           systemctl stop $name
           systemctl disable $name
           rm -f /etc/systemd/system/multi-user.target.wants/${name}.service
           rm -f $systemd_dir/${name}.service
        fi
        ;;
   'migrate')
 	echo "it's ready to migrate database...."
	$path_script/$name --config=$path_script/config/config.toml --migrate=true
	;;
   'start')
        echo "it's ready to start op...."
        systemctl start $name
        systemctl status $name
        echo "start end......"
        ;;
   'stop')
        echo "it's ready to check process..."
        systemctl stop $name
        systemctl status $name
        echo 'success to kill.'
        ;;
   'status')
        echo "it's ready to check status..."
        systemctl status $name
        ;;
    *)
        basename=`basename "$0"`
        echo "Usage: $basename  {install|uninstall|start|stop}  [ server options ]"
        exit 1
        ;;
esac
exit 1

mintty的Cmake配置

cmake_minimum_required(VERSION 3.12)
project(src C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -include std.h -Wall -Wextra -Wundef -Werror -mtune=atom -g -O0  -fstack-check")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc -MMD -MP")   

#当使用add_definitions设置时,res.rc文件是无法编译通过的,要使用CMAKE_C_FLAGS。
#add_definitions(-static-libgcc -MMD -MP -std=gnu99 -include std.h -Wall -Wextra -Wundef -Werror -mtune=atom -g -O0  -fstack-check)
message("CMAKE_C_COMPILER=${CMAKE_C_COMPILER}")
message("CMAKE_RC_COMPILER=${CMAKE_RC_COMPILER}")

include_directories(.)

link_directories(/usr/lib/w32api)

add_executable(mintty WIN32
        appinfo.h
        base64.c
        base64.h
        charset.c
        charset.h
        child.c
        child.h
        config.c
        config.h
        ctrls.c
        ctrls.h
        jumplist.c
        jumplist.h
        mcwidth.c
        minibidi.c
        minibidi.h
        print.h
        printers.c
        res.h
        sixel.c
        sixel.h
        sixel_hls.c
        sixel_hls.h
        std.c
        std.h
        term.c
        term.h
        termclip.c
        termline.c
        termmouse.c
        termout.c
        termpriv.h
        textprint.c
        win.h
        winclip.c
        winctrls.c
        winctrls.h
        windialog.c
        winids.h
        winimg.c
        winimg.h
        wininput.c
        winmain.c
        winpriv.h
        winsearch.c
        winsearch.h
        wintext.c
        wintip.c
        res.rc)

target_link_libraries(mintty libusp10.a libgdiplus.a libcomctl32.a  libimm32.a libwinmm.a libwinspool.a libole32.a libuuid.a)