夏天到了,树莓派的CPU温度也开始节节攀升,虽然我们也可以用云服务cosm来监控,但每5分钟采样一次精度不够高,每分钟采样一次则上传次数又太多了点。最好的方法还是使用tsar这样的工具本地高频(如每1分钟)采样,然后再定时将5分钟的均值上传到cosm绘图。
Tsar是淘宝的一个用来收集服务器系统和应用信息的采集报告工具,如收集服务器的系统信息(cpu,mem等),以及应用数据(nginx、swift等),收集到的数据存储在服务器磁盘上,可以随时查询历史信息,也可以将数据发送到nagios报警。Tsar能够比较方便的增加模块,只需要按照tsar的要求编写数据的采集函数和展现函数,就可以把自定义的模块加入到tsar中。
更新
[2013-04-14] mod_rpi已经被合并到了主干代码:https://github.com/alibaba/tsar/blob/master/modules/mod_rpi.c 只需要增加文件:/etc/tsar/conf.d/rpi.conf,内容为以下即可开始使用mod_rpi模块:
mod_rpi on
####add it to tsar default output
output_stdio_mod mod_rpi
mod_rpi模块开发方法
首先按照安装说明,见https://github.com/alibaba/tsar将tsar和tsardevel安装好。
首先运行下面的命令生成mod_rpi模块:
hugo@raspberrypi2 ~/projects/tsardevel $ tsardevel rpi
build:make
install:make install
uninstall:make uninstall
hugo@raspberrypi2 ~/projects/tsardevel $ ls rpi
Makefile mod_rpi.c mod_rpi.conf
然后修改mod_rpi.c,增加读取CPU温度的逻辑:
/*
* (C) 2010-2011 Alibaba Group Holding Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "tsar.h"
/*
* Structure for rpi infomation.
*/
struct stats_rpi {
unsigned int cpu_temp;
};
#define STATS_TEST_SIZE (sizeof(struct stats_rpi))
static char *rpi_usage = " --rpi Rapsberry Pi information (CPU temprature ...)";
static void read_rpi_stats(struct module *mod, char *parameter)
{
FILE *fp;
char buf[64];
memset(buf, 0, sizeof(buf));
struct stats_rpi st_rpi;
memset(&st_rpi, 0, sizeof(struct stats_rpi));
if ((fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r")) == NULL) {
return;
}
int cpu_temp;
fscanf(fp, "%d", &cpu_temp);
st_rpi.cpu_temp = cpu_temp;
int pos = sprintf(buf, "%u",
/* the store order is not same as read procedure */
st_rpi.cpu_temp);
buf[pos] = '\0';
set_mod_record(mod, buf);
fclose(fp);
return;
}
static struct mod_info rpi_info[] = {
{" temp", SUMMARY_BIT, 0, STATS_NULL}
};
static void set_rpi_record(struct module *mod, double st_array[],
U_64 pre_array[], U_64 cur_array[], int inter)
{
st_array[0] = cur_array[0]/1000.0;
}
void mod_register(struct module *mod)
{
register_mod_fileds(mod, "--rpi", rpi_usage, rpi_info, 1, read_rpi_stats, set_rpi_record);
}
最后make && sudo make install将mod_rpi自定义tsar模块安装好。













