#include #include #include #include #include #include #include #include #include #include using namespace std; enum DMI_STATE{ state_none, state_system, state_board, state_cpu, state_ram, state_other }; struct DMI_INFO{ struct { string manufacturer,product,version,serial,uuid; } system; struct { string manufacturer,product,version,serial; } board; struct { string socket,manufacturer,id,version,maxspeed,currentspeed,corecount,coreenabled; } cpu; struct { string bank,locator,manufacturer,partnumber,type,typedetail,speed,size,serial,assettag,rank; } ram; void reset(){ system.manufacturer = system.product = system.version = system.serial = system.uuid = ""; board.manufacturer = board.product = board.version = board.serial = ""; cpu.socket = cpu.manufacturer = cpu.id = cpu.version = cpu.maxspeed = cpu.currentspeed = cpu.corecount = cpu.coreenabled = ""; ram.bank = ram.locator = ram.manufacturer = ram.partnumber = ram.type = ram.typedetail = ram.speed = ram.size = ram.serial = ram.assettag = ram.rank = ""; } }; static bool splitline (const char *line, char splitcar, string &w1, string &w2) { bool ret = false; const char *pt = str_skip(line); char tmp[strlen(pt)+1]; strcpy (tmp,pt); char *ptpt = strchr(tmp,splitcar); if (ptpt != NULL){ *ptpt++ = '\0'; ptpt = str_skip (ptpt); strip_end (ptpt); char *ptb = ptpt; while (*ptb != '\0'){ if (*ptb == ' ') *ptb = '_'; ptb++; } w1 = tmp; w2 = ptpt; ret = true; } return ret; } static int dmidecode() { glocal DMI_STATE state=state_none; glocal DMI_INFO info; ("dmidecode -q",5); tlmp_error ("%s failed\n",command); tlmp_error ("%s: %s\n",command,line); return 0; if (isalpha(line[0])){ if (glocal.state == state_system){ printf ("system/%s/%s/%s/%s/%s\n" ,glocal.info.system.manufacturer.c_str() ,glocal.info.system.product.c_str() ,glocal.info.system.version.c_str() ,glocal.info.system.serial.c_str() ,glocal.info.system.uuid.c_str()); }else if (glocal.state == state_board){ printf ("board/%s/%s/%s/%s\n" ,glocal.info.board.manufacturer.c_str() ,glocal.info.board.product.c_str() ,glocal.info.board.version.c_str() ,glocal.info.board.serial.c_str()); }else if (glocal.state == state_cpu){ printf ("cpu/%s/%s/%s/%s/%s/%s/%s/%s\n" ,glocal.info.cpu.socket.c_str() ,glocal.info.cpu.manufacturer.c_str() ,glocal.info.cpu.id.c_str() ,glocal.info.cpu.version.c_str() ,glocal.info.cpu.maxspeed.c_str() ,glocal.info.cpu.currentspeed.c_str() ,glocal.info.cpu.corecount.c_str() ,glocal.info.cpu.coreenabled.c_str()); }else if (glocal.state == state_ram){ int size = atoi(glocal.info.ram.size.c_str()); if (size > 0){ printf ("ram/%s/%s/%s/%s/%s/%s/%s/%s/%s/%s/%s\n" ,glocal.info.ram.bank.c_str() ,glocal.info.ram.locator.c_str() ,glocal.info.ram.manufacturer.c_str() ,glocal.info.ram.partnumber.c_str() ,glocal.info.ram.type.c_str() ,glocal.info.ram.typedetail.c_str() ,glocal.info.ram.speed.c_str() ,glocal.info.ram.size.c_str() ,glocal.info.ram.serial.c_str() ,glocal.info.ram.assettag.c_str() ,glocal.info.ram.rank.c_str()); } } glocal.info.reset(); if (strcmp(line,"System Information")==0){ glocal.state = state_system; }else if (strcmp(line,"Base Board Information")==0){ glocal.state = state_board; }else if (strcmp(line,"Processor Information")==0){ glocal.state = state_cpu; }else if (strcmp(line,"Memory Device")==0){ glocal.state = state_ram; }else{ glocal.state = state_other; } }else{ string w1,w2; if (splitline(line,':',w1,w2)){ const char *tmp = w1.c_str(); if (glocal.state == state_system){ if (w1 == "Manufacturer"){ glocal.info.system.manufacturer = w2; }else if (strcmp(tmp,"Product Name")==0){ glocal.info.system.product = w2; }else if (strcmp(tmp,"Version")==0){ glocal.info.system.version = w2; }else if (strcmp(tmp,"Serial Number")==0){ glocal.info.system.serial = w2; }else if (strcmp(tmp,"UUID")==0){ glocal.info.system.uuid = w2; } }else if (glocal.state == state_board){ if (strcmp(tmp,"Manufacturer")==0){ glocal.info.board.manufacturer = w2; }else if (strcmp(tmp,"Product Name")==0){ glocal.info.board.product = w2; }else if (strcmp(tmp,"Version")==0){ glocal.info.board.version = w2; }else if (strcmp(tmp,"Serial Number")==0){ glocal.info.board.serial = w2; } }else if (glocal.state == state_cpu){ if (strcmp(tmp,"Socket Designation")==0){ glocal.info.cpu.socket = w2; }else if (strcmp(tmp,"Manufacturer")==0){ glocal.info.cpu.manufacturer = w2; }else if (strcmp(tmp,"ID")==0){ glocal.info.cpu.id = w2; }else if (strcmp(tmp,"Version")==0){ glocal.info.cpu.version = w2; }else if (strcmp(tmp,"Max Speed")==0){ glocal.info.cpu.maxspeed = w2; }else if (strcmp(tmp,"Current Speed")==0){ glocal.info.cpu.currentspeed = w2; }else if (strcmp(tmp,"Core Count")==0){ glocal.info.cpu.corecount = w2; }else if (strcmp(tmp,"Core Enabled")==0){ glocal.info.cpu.coreenabled = w2; } }else if (glocal.state == state_ram){ if (strcmp(tmp,"Size")==0){ glocal.info.ram.size = w2; }else if (strcmp(tmp,"Locator")==0){ glocal.info.ram.locator = w2; }else if (strcmp(tmp,"Bank Locator")==0){ glocal.info.ram.bank = w2; }else if (strcmp(tmp,"Type")==0){ glocal.info.ram.type = w2; }else if (strcmp(tmp,"Type Detail")==0){ glocal.info.ram.typedetail = w2; }else if (strcmp(tmp,"Speed")==0){ glocal.info.ram.speed = w2; }else if (strcmp(tmp,"Manufacturer")==0){ glocal.info.ram.manufacturer = w2; }else if (strcmp(tmp,"Serial Number")==0){ glocal.info.ram.serial = w2; }else if (strcmp(tmp,"Asset Tag")==0){ glocal.info.ram.assettag = w2; }else if (strcmp(tmp,"Part Number")==0){ glocal.info.ram.partnumber = w2; }else if (strcmp(tmp,"Rank")==0){ glocal.info.ram.rank = w2; } } } } return 0; return 0; } static void kernel () { ("/proc/meminfo",true); if (strncmp(line,"MemTotal:",9)==0){ const char *pt = str_skip (line+9); long mem = atol(pt); // Convert in GIG. The value reported in meminfo changes from // one kernel to the other. double fmem = (double)mem/(1024*1024); mem = (long)(fmem + 0.8); printf ("mem/%ldG\n",mem); } return 0; ("/proc/version",true); SSTRINGS words; int n = str_splitline(line,' ',words); if (n > 3) printf ("kernel/%s\n",words.getitem(2)->c_str()); return 0; } struct SCSIID{ string vendor,model,revision,serial,wwn,scsi_serial; }; static void disks () { ("cd /sys/block && ls -d sd?",5); glocal SCSIID scsi; glocal bool lineseen = false; // Deux versions de la commande for (int i=0; i<2; i++){ SSTRING tmp; if (i==0){ tmp.setfromf ("/lib/udev/scsi_id --export --replace-whitespace --whitelisted --device=/dev/%s",line); }else{ tmp.setfromf ("/lib/udev/scsi_id --export --replace-whitespace --whitelisted --devpath=/block/%s",line); } debug_printf ("Exec %s\n",tmp.c_str()); (tmp.c_str(),5); glocal.lineseen = true; string w1,w2; if (splitline(line,'=',w1,w2)){ if (w1 == "ID_VENDOR"){ glocal.scsi.vendor = w2; }else if (w1 == "ID_MODEL"){ glocal.scsi.model = w2; }else if (w1 == "ID_REVISION"){ glocal.scsi.revision = w2; }else if (w1 == "ID_SERIAL"){ glocal.scsi.serial = w2; }else if (w1 == "ID_WWN"){ glocal.scsi.wwn = w2; }else if (w1 == "ID_SCSI_SERIAL"){ glocal.scsi.scsi_serial = w2; } } return 0; if (glocal.lineseen) break; } printf ("disk/%s/%s/%s/%s/%s/%s/%s\n",line ,glocal.scsi.vendor.c_str() ,glocal.scsi.model.c_str() ,glocal.scsi.revision.c_str() ,glocal.scsi.serial.c_str() ,glocal.scsi.wwn.c_str() ,glocal.scsi.scsi_serial.c_str()); return 0; } static int ifconfig_gethwaddr ( const char *ifname, string &hwaddr) { int ret = -1; int skfd = socket(AF_INET, SOCK_DGRAM, 0); if (skfd != -1){ struct ifreq ifr; strcpy(ifr.ifr_name, ifname); if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0){ ret = 0; unsigned char mac_address[6]; memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6); char tmp[6*3+1]; snprintf (tmp,sizeof(tmp)-1,"%02x:%02x:%02x:%02x:%02x:%02x" ,mac_address[0] ,mac_address[1] ,mac_address[2] ,mac_address[3] ,mac_address[4] ,mac_address[5]); hwaddr = tmp; } close (skfd); } return ret; } static void network() { ("/proc/net/dev",true); if (noline > 1){ const char *pt = str_skip(line); const char *end = strchr(pt,':'); if (end != NULL){ string dev(pt,end-pt); string macaddr; if (ifconfig_gethwaddr(dev.c_str(),macaddr)!=-1){ if (macaddr != "00:00:00:00:00:00"){ printf ("net/%s/%s\n",dev.c_str(),macaddr.c_str()); } } } } return 0; if (file_exist("/dev/ipmi0")){ ("ipmitool lan print 2>/dev/null",10); if(strncmp(line,"MAC Address",11)==0){ const char *pt = strchr(line,':'); if (pt != NULL){ pt = str_skip(pt+1); printf ("net/ipmi/%s\n",pt); } } return 0; } } int main (int argc, char *argv[]) { glocal int ret = -1; glocal.ret = (argc,argv); setproginfo ("logstory-scan",VERSION, "Estract keys for hardware and resources on this server\n" "All keys are printed on standard outout\n"); dmidecode(); kernel(); disks(); network(); return 0; return glocal.ret; }