"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/linux/raidmeter.cc" (11 Jul 2020, 3454 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "raidmeter.cc" see the Fossies "Dox" file reference documentation.

    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");
   30     setNumBits(disknum);
   31   }
   32   total_ = 100.0;
   33 }
   34 
   35 RAIDMeter::~RAIDMeter( void ){
   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){
   49     setfieldcolor(0,doneColor_);
   50     setfieldcolor(1,todoColor_);
   51   }else{
   52     setfieldcolor(0,completeColor_);
   53   }
   54   setUsed(fields_[0], total_);
   55   BitFieldMeter::checkevent();
   56 }
   57 
   58 void RAIDMeter::checkResources( void ){
   59   BitFieldMeter::checkResources();
   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"));
   66   setfieldcolor( 0, doneColor_ );
   67   setfieldcolor( 1, todoColor_ );
   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 
  120 void RAIDMeter::getRAIDstate( void ){
  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 }