现在很多rails的应用都用monit来做监控,monit提供了一个简单的web server可以很好的远程看到监控资源的情况。
但是有一个问题就是这个web server实现的比较简单,没有考虑多语言的支持。不过,我们可以通过简单修改一下monit的源代码,让它支持中文。
修改的方法有两个:
1、http/cervlet.h里面HEAD_HTML这个宏,是monit页面的html头,可以考虑增加:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
然后,用unicode的方式来编辑monit的控制文件,这样,就能够在页面上用unicode的方式来访问了
这个方法有一个不方便的地方就是必须要用unicode的方式来编辑控制文件。
2、对cervlet.c进行修改,用iconv库把url进行编码转换。在cervlet.c增加如下函数
- #include <iconv.h>
- int code_convert( char *from_charset, char *to_charset, char *inbuf, int inlen, char *outbuf, int outlen)
- {
- iconv_t cd;
- //int rc;
- char **pin = &inbuf;
- char **pout = &outbuf;
- cd = iconv_open(to_charset,from_charset);
- if (cd==0) return -1;
- memset(outbuf, 0, outlen);
- if (iconv(cd, ( const char **)pin, (unsigned int *)&inlen, pout, &outlen) == -1)
- {
- perror( "iconv error:" );
- return -1;
- }
- iconv_close(cd);
- return 0;
- }
再把handle_action函数的前面几行进行修改
- static void handle_action(HttpRequest req, HttpResponse res) {
- char *org_name= req->url;
- const char *action= get_parameter(req, "action" );
- char new_name[512];
- code_convert( "UTF-8" , "GBK" , org_name, strlen(org_name), new_name, 512);
- char *name = new_name;
重新编译之后就可以使用了,这个方式的缺点是修改的量有点大,但是以后使用起来就比较方便了。
注:以上代码基于monit-4.6