xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

raidmeter.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 1999, 2006 by Thomas Waldmann ( ThomasWaldmann@gmx.de )
3 // based on work of Mike Romberg ( mike.romberg@noaa.gov )
4 //
5 // This file may be distributed under terms of the GPL
6 //
7 
8 #include "raidmeter.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <fstream>
13 #include <sstream>
14 #include <iostream>
15 
16 static const char *RAIDFILE = "/proc/mdstat";
17 
18 
19 RAIDMeter::RAIDMeter( XOSView *parent, int raiddev)
20  : BitFieldMeter( parent, 1, 2, "RAID") {
21  _raiddev = raiddev;
22  getRAIDstate();
23  if(disknum<1)
24  disableMeter();
25  std::ostringstream os;
26  os << "MD" << raiddev << std::ends;
27  legend(os.str().c_str());
28  if(disknum>=1){
29  setfieldlegend("Done/ToDo");
31  }
32  total_ = 100.0;
33 }
34 
36 }
37 
38 void RAIDMeter::checkevent( void ){
39 
40  getRAIDstate();
41 
42  for ( int i = 0 ; i < disknum ; i++ ){
43  bits_[i] = (working_map[i]=='+');
44  }
45  fields_[0]=100.0;
46  sscanf(resync_state, "resync=%lf", &fields_[0] );
47  fields_[1] = total_ - fields_[1];
48  if(fields_[0]<100.0){
51  }else{
53  }
54  setUsed(fields_[0], total_);
56 }
57 
60  onColor_ = parent_->allocColor( parent_->getResource( "RAIDdiskOnlineColor" ) );
61  offColor_ = parent_->allocColor( parent_->getResource( "RAIDdiskFailureColor" ) );
62  doneColor_ = parent_->allocColor( parent_->getResource( "RAIDresyncdoneColor" ) );
63  todoColor_ = parent_->allocColor( parent_->getResource( "RAIDresynctodoColor" ) );
64  completeColor_= parent_->allocColor( parent_->getResource( "RAIDresynccompleteColor" ) );
65  priority_ = atoi(parent_->getResource("RAIDPriority"));
68  SetUsedFormat(parent_->getResource( "RAIDUsedFormat" ) );
69 }
70 
71 // parser for /proc/mdstat
72 
73 int RAIDMeter::find1(const char *key, const char *findwhat, int num1){
74  char buf[80];
75  int rc;
76  std::ostringstream os;
77  os << findwhat << "." << num1 << std::ends;
78  strncpy(buf, os.str().c_str(), 80);
79  buf[79] = '\0';
80  rc=!strncmp(buf,key, 80);
81  return rc;
82 }
83 
84 int RAIDMeter::find2(const char *key, const char *findwhat,
85  int num1, int num2){
86  char buf[80];
87  int rc;
88  std::ostringstream os;
89  os << findwhat << "." << num1 << "." << num2 << std::ends;
90  strncpy(buf, os.str().c_str(), 80);
91  buf[79] = '\0';
92  rc=!strncmp(buf,key, 80);
93  return rc;
94 }
95 
96 int RAIDMeter::raidparse(char *cp){
97  char *key, *val;
98  key=strtok(cp," \n");
99  val=strtok(NULL," \n");
100  if(key==NULL) return 1;
101 
102  if(find1(key,"md_state",_raiddev)){
103  if(val) strcpy(state,val);
104  }else
105  if(find1(key,"md_type",_raiddev)){
106  if(val) strcpy(type,val);
107  }else
108  if(find1(key,"md_disk_count",_raiddev)){
109  if(val) disknum=atoi(val);
110  }else
111  if(find1(key,"md_working_disk_map",_raiddev)){
112  if(val) strcpy(working_map,val);
113  }else
114  if(find1(key,"md_resync_status",_raiddev)){
115  if(val) strcpy(resync_state,val);
116  }
117  return 0;
118 }
119 
121  std::ifstream raidfile( RAIDFILE );
122  char l[256];
123 
124  if ( !raidfile ){
125  std::cerr <<"Can not open file : " <<RAIDFILE << std::endl;
126  exit( 1 );
127  }
128 
129  do{
130  raidfile.getline(l,256);
131  }while((raidparse(l)==0) && (!raidfile.eof()));
132 
133 // printf("md0 %s %s %s resync: %s\n",type,state,working_map,resync_state);
134 }
void checkevent(void)
void setUsed(double val, double total)
double * fields_
Definition: bitfieldmeter.h:47
void setNumBits(int n)
void setfieldlegend(const char *fieldlegend)
void SetUsedFormat(const char *const str)
unsigned long onColor_
Definition: bitfieldmeter.h:58
virtual void checkResources(void)
unsigned long offColor_
Definition: bitfieldmeter.h:58
void disableMeter(void)
void setfieldcolor(int field, const char *color)
int priority_
Definition: meter.h:54
const char * legend(void)
Definition: meter.h:27
XOSView * parent_
Definition: meter.h:52
int find1(const char *key, const char *findwhat, int num1)
Definition: raidmeter.cc:73
char working_map[20]
Definition: raidmeter.h:33
~RAIDMeter(void)
Definition: raidmeter.cc:35
unsigned long todoColor_
Definition: raidmeter.h:37
int _raiddev
Definition: raidmeter.h:28
int disknum
Definition: raidmeter.h:35
void checkevent(void)
Definition: raidmeter.cc:38
char state[20]
Definition: raidmeter.h:31
unsigned long doneColor_
Definition: raidmeter.h:37
int find2(const char *key, const char *findwhat, int num1, int num2)
Definition: raidmeter.cc:84
int raidparse(char *cp)
Definition: raidmeter.cc:96
char type[20]
Definition: raidmeter.h:32
void checkResources(void)
Definition: raidmeter.cc:58
unsigned long completeColor_
Definition: raidmeter.h:37
RAIDMeter(XOSView *parent, int raiddev=0)
Definition: raidmeter.cc:19
char resync_state[20]
Definition: raidmeter.h:34
void getRAIDstate(void)
Definition: raidmeter.cc:120
unsigned long allocColor(const char *name)
Definition: xwin.cc:383
const char * getResource(const char *name)
Definition: xwin.cc:362
static const char * RAIDFILE
Definition: raidmeter.cc:16