当前位置: &
& 查看源码
rrd_update.c
rrd_update.c:源码内容
/*****************************************************************************
&*&RRDtool&1.0.50&&Copyright&Tobias&Oetiker,&1997&-&2000
&*****************************************************************************
&*&rrd_update.c&&RRD&Update&Function
&*****************************************************************************
&*&$Id:&rrd_update.c,v&1.1.1.1&&10:21:38&oetiker&Exp&$
&*&$Log:&rrd_update.c,v&$
&*&Revision&1.1.1.1&&&10:21:38&&oetiker
&*&Intial&Import
&*****************************************************************************/
#include&&rrd_tool.h&
#include&&sys/types.h&
#include&&fcntl.h&
#ifdef&WIN32
&#include&&sys/locking.h&
&#include&&sys/stat.h&
&#include&&io.h&
/*&Prototypes&*/
int&LockRRD(FILE&*rrd_file);
/*#define&DEBUG&*/
#ifdef&STANDALONE
main(int&argc,&char&**argv){
&&&&&&&&rrd_update(argc,argv);
&&&&&&&&if&(rrd_test_error())&{
&&&&&&&&&&&&&&&&printf(&RRDtool&1.0.50&&Copyright&&by&Tobias&Oetiker&&tobi@oetiker.ch&nn&
&&&&&&&&&&&&&&&&&&&&&&&&&Usage:&rrdupdate&filenamen&
&&&&&&&&&&&&&&&&&&&&&&&&&ttt[--template|-t&ds-name:ds-name:...]n&
&&&&&&&&&&&&&&&&&&&&&&&&&ttttime|N:value[:value...]nn&
&&&&&&&&&&&&&&&&&&&&&&&&&ttt[&time:value[:value...]&..]nn&);
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&printf(&ERROR:&%sn&,rrd_get_error());
&&&&&&&&&&&&&&&&rrd_clear_error();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&return&1;
&&&&&&&&return&0;
rrd_update(int&argc,&char&**argv)
&&&&int&&&&&&&&&&&&&&arg_i&=&2;
&&&&long&&&&&&&&&&&&&i,ii,
&&&&unsigned&long&&&&rra_&&&&&&&&&&/*&byte&pointer&to&the&rra
&&*&area&in&the&rrd&file.&&this
&&*&pointer&never&changes&value&*/
&&&&unsigned&long&&&&rra_&&&&&&&&&&/*&byte&pointer&to&the&rra
&&*&area&in&the&rrd&file.&&this
&&*&pointer&changes&as&each&rrd&is
&&*&processed.&*/
&&&&unsigned&long&&&&rra_&&&&&&&&/*&byte&pointer&to&the¤t&write
&&*&spot&in&the&rrd&file.&*/
&&&&unsigned&long&&&&rra_pos_&&&&&&&&/*&temporary&byte&pointer.&*/
&&&&unsigned&long&&&&interval,
pre_int,post_&&&&&&&&&&&&&&&&/*&interval&between&this&and
&&*&the&last&run&*/
&&&&unsigned&long&&&&proc_pdp_&&&&&&&&/*&which&pdp_st&was&the&last
&&*&to&be&processed&*/
&&&&unsigned&long&&&&occu_pdp_&&&&&&&&/*&when&was&the&pdp_st
&&*&before&the&last&update
&&*&time&*/
&&&&unsigned&long&&&&proc_pdp_&&&&&&&/*&how&old&was&the&data&in
&&*&the&pdp&prep&area&when&it
&&*&was&last&updated&*/
&&&&unsigned&long&&&&occu_pdp_&&&&&&&/*&how&long&ago&was&the&last
&&*&pdp_step&time&*/
&&&&unsigned&long&&&&pdp_&&&&&&&&&&&&&/*&helper&for&cdp_prep&
&&*&processing&*/
&&&&rrd_value_t&&&&&&*pdp_&&&&&&&&&&&/*&prepare&the&incoming&data
&&*&to&be&added&the&the
&&*&existing&entry&*/
&&&&rrd_value_t&&&&&&*pdp_&&&&&&&&&&/*&prepare&the&pdp&values&
&&*&to&be&added&the&the
&&*&cdp&values&*/
&&&&long&&&&&&&&&&&&&*tmpl_&&&&&&&&&&/*&index&representing&the&settings
&&&&transported&by&the&template&index&*/
&&&&long&&&&&&&&&&&&&tmpl_cnt&=&2;&&&&&&&/*&time&and&data&*/
&&&&FILE&&&&&&&&&&&&&*rrd_
&&&&rrd_t&&&&&&&&&&&&
&&&&time_t&&&&&&&&&&¤t_time&=&time(NULL);
&&&&char&&&&&&&&&&&&&**
&&&&int&&&&&&&&&&&&&&wrote_to_file&=&0;
&&&&char&&&&&&&&&&&&&*template&=&NULL;&&&&&&&&&&
&&&&char&&&&&&&&&&&&&*&/*&used&in&the&conversion&*/
&&&&while&(1)&{
static&struct&option&long_options[]&=
&&&&{&template&,&&&&&&required_argument,&0,&'t'},
&&&&{0,0,0,0}
int&option_index&=&0;
opt&=&getopt_long(argc,&argv,&&t:&,&
&&long_options,&&option_index);
if&(opt&==&EOF)
switch(opt)&{
&&&&template&=&
&&&&rrd_set_error(&unknown&option&'%s'&,argv[optind-1]);
&&&&return(-1);
&&&&/*&need&at&least&2&arguments:&filename,&data.&*/
&&&&if&(argc-optind&&&2)&{
rrd_set_error(&Not&enough&arguments&);
return&-1;
&&&&if(rrd_open(argv[optind],&rrd_file,&rrd,&RRD_READWRITE)==-1){
return&-1;
&&&&rra_current&=&rra_start&=&rra_begin&=&ftell(rrd_file);
&&&&/*&This&is&defined&in&the&ANSI&C&standard,§ion&7.9.5.3:
&&&&&&&&When&a&file&is&opened&with&udpate&mode&('+'&as&the&second
&&&&&&&&or&third&character&in&the&...&list&of&mode&argument
&&&&&&&&variables),&both&input&and&output&may&be&performed&on&the
&&&&&&&&associated&stream.&&However,&...&&input&may¬&be&directly
&&&&&&&&followed&by&output&without&an&intervening&call&to&a&file
&&&&&&&&positioning&function,&unless&the&input&operation&encounters
&&&&&&&&end-of-file.&*/
&&&&fseek(rrd_file,&0,&SEEK_CUR);
&&&&/*&get&exclusive&lock&to&whole&file.
&&&&&*&lock&gets&removed&when&we&close&the&file.
&&&&if&(LockRRD(rrd_file)&!=&0)&{
&&&&&&rrd_set_error(&could¬&lock&RRD&);
&&&&&&rrd_free(&rrd);
&&&&&&fclose(rrd_file);
&&&&&&return(-1);&&&
&&&&if((updvals&=&malloc(&sizeof(char*)&*&(rrd.stat_head-&ds_cnt+1)))==NULL){
rrd_set_error(&allocating&updvals&pointer&array&);
rrd_free(&rrd);
&&&&&&&&fclose(rrd_file);
return(-1);
&&&&if&((pdp_temp&=&malloc(sizeof(rrd_value_t)
&&&*rrd.stat_head-&ds_cnt))==NULL){
rrd_set_error(&allocating&pdp_temp&...&);
free(updvals);
rrd_free(&rrd);
&&&&&&&&fclose(rrd_file);
return(-1);
&&&&if&((tmpl_idx&=&malloc(sizeof(unsigned&long)
&&&*(rrd.stat_head-&ds_cnt+1)))==NULL){
rrd_set_error(&allocating&tmpl_idx&...&);
free(pdp_temp);
free(updvals);
rrd_free(&rrd);
&&&&&&&&fclose(rrd_file);
return(-1);
&&&&/*&initialize&template&redirector&*/
&&&&/*&default&config
&&&&&&&tmpl_idx[0]&-&&0;&(time)
&&&&&&&tmpl_idx[1]&-&&1;&(DS&0)
&&&&&&&tmpl_idx[2]&-&&2;&(DS&1)
&&&&&&&tmpl_idx[3]&-&&3;&(DS&2)
&&&&&&&...&*/
&&&&for&(i=0;i&=rrd.stat_head-&ds_i++)&tmpl_idx[i]=i;
&&&&tmpl_cnt=rrd.stat_head-&ds_cnt+1;
&&&&if&(template)&{
tmpl_cnt&=&1;&/*&the&first&entry&is&the&time&*/
tmpl_len&=&strlen(template);
for(i=0;i&=tmpl_len&;i++)&{
&&&&if&(template[i]&==&':'&||&template[i]&==&'