在Nagios的状态图中输出中文以及透明文字(Nagios笔记)
yang | 08 六月, 2010 16:16
一、中文输出
在Nagios的Statusmap中,中文字符会是乱码,可从statusmap.c着手,找到draw_text函数,可以看到,在该函数内使用php的gd库函数 gdImageString(),如果使用支持中文TTF的函数替换该函数,则可支持中文字符。
原函数如下:
/* draws text */
void draw_text(char *buffer,int x,int y,int text_color){
int string_width=0;
int string_height=0;
/* write the string to the generated image... */
string_height=gdFontSmall->h;
string_width=gdFontSmall->w*strlen(buffer);
if(layout_method!=LAYOUT_CIRCULAR_MARKUP)
gdImageFilledRectangle(map_image,x-(string_width/2)-2,y-(2*string_height),x+(string_width/2)+2,y-string_height,color_white);
gdImageString(map_image,gdFontSmall,x-(string_width/2),y-(2*string_height),(unsigned char *)buffer,text_color);
return;
}
修改后的函数如下:
/* draws text */
void draw_text(char *buffer,int x,int y,int text_color){
int string_width=0;
int string_height=0;
int brect[8];
/* write the string to the generated image... */
string_height=gdFontSmall->h;
string_width=gdFontSmall->w*strlen(buffer);
if(layout_method!=LAYOUT_CIRCULAR_MARKUP)
gdImageFilledRectangle(map_image,x-(string_width/2)-2,y-(2*string_height),x+(string_width/2)+2,y-string_height,color_white);
/*Matt Edited Start*/
gdImageStringTTF(map_image,&brect[0],text_color,"/usr/share/fonts/chinese/TrueType/uming.ttf",10,0.0,x-(string_width/2),y-(2*string_height),buffer);
/*gdImageString(map_image,gdFontSmall,x-(string_width/2),y-(2*string_height),(unsigned char *)buffer,text_color);*/
/*Matt Edited End */
return;
}
其中红色的代码是新增加的,绿色的是被注释掉的。修改后重新configure; make all && make install 即可
二、使用透明文字
在状态图中的文字除了使用Circular Markup模式外,其他模式的文字都被加上了白色的背景,十分影响美观,要使用透明文字同样需向draw_text下手。
原函数如下:
/* draws text */
void draw_text(char *buffer,int x,int y,int text_color){
int string_width=0;
int string_height=0;
int brect[8];
/* write the string to the generated image... */
string_height=gdFontSmall->h;
string_width=gdFontSmall->w*strlen(buffer);
if(layout_method!=LAYOUT_CIRCULAR_MARKUP)
gdImageFilledRectangle(map_image,x-(string_width/2)-2,y-(2*string_height),x+(string_width/2)+2,y-string_height,color_white);
/*Matt Edited Start*/
gdImageStringTTF(map_image,&brect[0],text_color,"/usr/share/fonts/chinese/TrueType/uming.ttf",10,0.0,x-(string_width/2),y-(2*string_height),buffer);
/*gdImageString(map_image,gdFontSmall,x-(string_width/2),y-(2*string_height),(unsigned char *)buffer,text_color);*/
/*Matt Edited End */
return;
}
修改后的函数如下:
/* draws text */
void draw_text(char *buffer,int x,int y,int text_color){
int string_width=0;
int string_height=0;
int brect[8];
/* write the string to the generated image... */
string_height=gdFontSmall->h;
string_width=gdFontSmall->w*strlen(buffer);
/* if(layout_method!=LAYOUT_CIRCULAR_MARKUP) */
/* gdImageFilledRectangle(map_image,x-(string_width/2)-2,y-(2*string_height),x+(string_width/2)+2,y-string_height,color_white); */
/*Matt Edited Start*/
gdImageStringTTF(map_image,&brect[0],text_color,"/usr/share/fonts/chinese/TrueType/uming.ttf",10,0.0,x-(string_width/2),y-(2*string_height),buffer);
/*gdImageString(map_image,gdFontSmall,x-(string_width/2),y-(2*string_height),(unsigned char *)buffer,text_color);*/
/*Matt Edited End */
return;
}
可以看到,只需将绿色部分注释掉,draw_text函数在输出文字的时候就不会主动增加白底了。