/* * Get MegaRAID logical drive information. */ MRLDRIVE *getMRLogicalDrive(int adapter_id, int ldrive_id) { MRLDRIVE *logical_drive = 0; FILE *megacli = NULL; char *command = NULL, *strtok_result = NULL; char line[MAX_MC_LINE] = {0}; int status = 0; logical_drive = (MRLDRIVE *) calloc(1, sizeof(MRLDRIVE)); if (logical_drive != NULL) { logical_drive->adapter_id = adapter_id; logical_drive->ldrive_id = ldrive_id; /* MegaCLI command */ SAFE_ASPRINTF(&command, "%s -LDInfo -L%d -a%d -NoLog 2>&1", MEGACLI_BIN, ldrive_id, adapter_id); megacli = popen(command, "r"); /* Loop over command output */ while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "RAID Level :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(logical_drive->raid_lvl, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Size :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(logical_drive->size, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "State :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(logical_drive->state, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Strip Size :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(logical_drive->strip_size, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Number Of Drives :")) { sscanf(line, "%*s %*s %*s %*s %d", &logical_drive->drive_cnt); } } /* Done with MegaCLI */ status = pclose(megacli); FREE_NULL(command); if (status != 0) { FREE_NULL(logical_drive); return NULL; } } /* Done */ return logical_drive; }
int main (int argc, char* argv[]) { char str[] = {"Every good boy does fine."}; char str2[30] = {"Every good boy does fine."}; char str3[30] = {""}; //Test Word Count int wordCount = strWordCount(str); printf("Word count: %d\n", wordCount); //end test word count // //Test str to upper. strToUpper(str, str2); printStringArray(str); printStringArray(str2); //end test str to upper. // //Test str Strip char c = 'o'; strStrip(str, c, str2); printStringArray(str); printStringArray(str2); //end test str Strip // //Test str Substring char d[] = {"boy"}; printf("Where does the word boy start? %d", strIsSubstring(str, d)); //end test str is Substring // //Test strSubstitute char e, g; e = 'g'; g = 'o'; strSubstitute(str, e, g, str2); printStringArray(str); printStringArray(str2); //end test strSubstitute // //Test strReverse strReverse(str, str2); printStringArray(str); printStringArray(str2); //Test Palindrome char tacocat[] = {"tacocat"}; printf("Is tacocat a Palindrome? %d", strPalindrome(tacocat)); //end palindrome test // //Test strSplit strSplit (str, 'o', 3, str3, str2); printf("The starting str\n"); printStringArray(str); printf("The second half of str\n"); printStringArray(str2); printf("The first half of str\n"); printStringArray(str3); }
/* * Get adapter attributes from MegaCLI. */ MRADAPTER *getMRAdapter(int adapter_id) { MRADAPTER *adapter = 0; FILE *megacli = NULL; char *command = NULL, *strtok_result = NULL; char line[MAX_MC_LINE] = {0}; int status = 0; adapter = (MRADAPTER *) calloc(1, sizeof(MRADAPTER)); if (adapter != NULL) { adapter->adapter_id = adapter_id; /* MegaCLI command */ SAFE_ASPRINTF(&command, "%s -AdpAllInfo -a%d -NoLog 2>&1", MEGACLI_BIN, adapter_id); megacli = popen(command, "r"); /* Loop over command output */ while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "Product Name :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(adapter->prod_name, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Serial No :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(adapter->serial, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "FW Package Build:")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(adapter->firmware, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "BBU :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(adapter->bbu, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Memory Size :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(adapter->memory, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Host Interface :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(adapter->interface, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Virtual Drives :")) { sscanf(line, "%*s %*s %*s %d", &adapter->logical_drv_cnt); } else if (strstr(line, " Disks :")) { sscanf(line, "%*s %*s %d", &adapter->disk_cnt); } else if (strstr(line, "Cluster Permitted :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(adapter->cluster, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Cluster Active :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(adapter->cluster_on, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } } /* Done with MegaCLI */ status = pclose(megacli); FREE_NULL(command); if (status != 0) { FREE_NULL(adapter); return NULL; } } /* Done */ return adapter; }
/* * Get MegaRAID logical (virtual) drive properties. */ MRLDPROPS *getMRLDProps(int adapter_id, int ldrive_id) { MRLDPROPS *ld_props = 0; FILE *megacli = NULL; char *command = NULL, *strtok_result = NULL; char line[MAX_MC_LINE] = {0}, temp_str[MAX_MC_LINE] = {0}; int status = 0; ld_props = (MRLDPROPS *) calloc(1, sizeof(MRLDPROPS)); if (ld_props != NULL) { ld_props->adapter_id = adapter_id; ld_props->ldrive_id = ldrive_id; /* Get cache policies */ SAFE_ASPRINTF(&command, "%s -LDGetProp -Cache -L%d -a%d -NoLog 2>&1", MEGACLI_BIN, ldrive_id, adapter_id); megacli = popen(command, "r"); while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "Cache Policy:")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strtok_result = strtok(NULL, ":"); strtok_result = strtok(NULL, ":"); strcpy(temp_str, strtok_result); /* Write policy */ strtok_result = strtok(temp_str, ","); if (strstr(strStrip(strtok_result), "WriteThrough")) { strncpy(ld_props->write_policy, "WT", MAX_MR_ATTR_SIZE); } else if (strstr(strStrip(strtok_result), "WriteBack")) { strncpy(ld_props->write_policy, "WB", MAX_MR_ATTR_SIZE); } else { strncpy(ld_props->write_policy, "UNKNOWN", MAX_MR_ATTR_SIZE); } /* Read policy */ strtok_result = strtok(NULL, ","); if (strstr(strStrip(strtok_result), "ReadAheadNone")) { strncpy(ld_props->read_policy, "NORA", MAX_MR_ATTR_SIZE); } else if (strstr(strStrip(strtok_result), "ReadAhead")) { strncpy(ld_props->read_policy, "RA", MAX_MR_ATTR_SIZE); } else if (strstr(strStrip(strtok_result), "ReadAdaptive")) { strncpy(ld_props->read_policy, "ADRA", MAX_MR_ATTR_SIZE); } else { strncpy(ld_props->read_policy, "UNKNOWN", MAX_MR_ATTR_SIZE); } /* Cache policy */ strtok_result = strtok(NULL, ","); if (strstr(strStrip(strtok_result), "Direct")) { strncpy(ld_props->cache_policy, "Direct", MAX_MR_ATTR_SIZE); } else if (strstr(strStrip(strtok_result), "Cached")) { strncpy(ld_props->cache_policy, "Cached", MAX_MR_ATTR_SIZE); } else { strncpy(ld_props->cache_policy, "UNKNOWN", MAX_MR_ATTR_SIZE); } /* BBU cache policy */ strtok_result = strtok(NULL, ","); if (strstr(strStrip(strtok_result), "No Write Cache if bad BBU")) { strncpy(ld_props->bbu_cache_policy, "NoCachedBadBBU", MAX_MR_ATTR_SIZE); } else if (strstr(strStrip(strtok_result), "Write Cache OK if bad BBU")) { strncpy(ld_props->bbu_cache_policy, "CachedBadBBU", MAX_MR_ATTR_SIZE); } else { strncpy(ld_props->bbu_cache_policy, "UNKNOWN", MAX_MR_ATTR_SIZE); } } } status = pclose(megacli); FREE_NULL(command); if (status != 0) { return NULL; } /* Get Name */ SAFE_ASPRINTF(&command, "%s -LDGetProp -Name -L%d -a%d -NoLog 2>&1", MEGACLI_BIN, ldrive_id, adapter_id); megacli = popen(command, "r"); while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "Name:")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strtok_result = strtok(NULL, ":"); strtok_result = strtok(NULL, ":"); strncpy(ld_props->name, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } } status = pclose(megacli); FREE_NULL(command); if (status != 0) { return NULL; } } /* Done */ return ld_props; }
/* * Get MegaRAID enclosure information; not happy about how this is * implemented -- if LSI could have just made all of the MegaCLI commands * uniform (eg, be able to specify an enclosure like you specify an adapter)! */ MRENCL *getMREnclosure(int adapter_id, int encl_id) { MRENCL *enclosure = 0; FILE *megacli = NULL; char *command = NULL, *strtok_result = NULL; char line[MAX_MC_LINE] = {0}; int status = 0; int counters[] = {0, 0, 0, 0, 0, 0, 0}; enclosure = (MRENCL *) calloc(1, sizeof(MRENCL)); if (enclosure != NULL) { enclosure->adapter_id = adapter_id; /* MegaCLI command */ SAFE_ASPRINTF(&command, "%s -EncInfo -a%d -NoLog 2>&1", MEGACLI_BIN, adapter_id); megacli = popen(command, "r"); /* Loop over command output */ while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, " Device ID :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); if (counters[0] == encl_id) sscanf(strtok_result, " %d", &enclosure->device_id); counters[0]++; } else if (strstr(line, " Number of Slots :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); if (counters[1] == encl_id) sscanf(strtok_result, " %d", &enclosure->slots); counters[1]++; } else if (strstr(line, " Number of Power Supplies :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); if (counters[2] == encl_id) sscanf(strtok_result, " %d", &enclosure->power_supps); counters[2]++; } else if (strstr(line, " Number of Fans :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); if (counters[3] == encl_id) sscanf(strtok_result, " %d", &enclosure->fans); counters[3]++; } else if (strstr(line, " Status :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); if (counters[4] == encl_id) strncpy(enclosure->status, strStrip(strtok_result), MAX_MR_ATTR_SIZE); counters[4]++; } else if (strstr(line, " Vendor Identification :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); if (counters[5] == encl_id) strncpy(enclosure->vendor, strStrip(strtok_result), MAX_MR_ATTR_SIZE); counters[5]++; } else if (strstr(line, " Product Identification :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); if (counters[6] == encl_id) strncpy(enclosure->product, strStrip(strtok_result), MAX_MR_ATTR_SIZE); counters[6]++; } } /* Done with MegaCLI */ status = pclose(megacli); FREE_NULL(command); if (status != 0) { FREE_NULL(enclosure); return NULL; } } /* Done */ return enclosure; }
/* * Get MegaRAID disk information. */ MRDISK *getMRDisk(int adapter_id, int encl_id, int slot) { MRDISK *disk = 0; FILE *megacli = NULL; char *command = NULL, *strtok_result = NULL; char line[MAX_MC_LINE] = {0}; int status = 0; disk = (MRDISK *) calloc(1, sizeof(MRDISK)); if (disk != NULL) { disk->adapter_id = adapter_id; disk->present = TRUE; disk->part_of_ld = FALSE; /* MegaCLI command */ SAFE_ASPRINTF(&command, "%s -pdInfo -PhysDrv[%d:%d] -a%d -NoLog 2>&1", MEGACLI_BIN, encl_id, slot, adapter_id); megacli = popen(command, "r"); /* Loop over command output */ while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "is not found.")) { /* A disk isn't present in the specified slot */ disk->present = FALSE; continue; } else if (strstr(line, "Enclosure Device ID:")) { sscanf(line, "%*s %*s %*s %d", &disk->enclosure_id); } else if (strstr(line, "Slot Number:")) { sscanf(line, "%*s %*s %d", &disk->slot_num); } else if (strstr(line, "PD Type:")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(disk->pd_type, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Raw Size:")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(disk->raw_size, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Firmware state:")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(disk->state, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Inquiry Data:")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(disk->inquiry, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Link Speed:")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strncpy(disk->speed, strStrip(strtok_result), MAX_MR_ATTR_SIZE); } else if (strstr(line, "Drive's position:")) { /* If the PD entry contains the string above, then its * part of a logical drive (LD). */ disk->part_of_ld = TRUE; } } /* Done with MegaCLI */ status = pclose(megacli); FREE_NULL(command); if (status != 0) { FREE_NULL(disk); return NULL; } } /* Done */ return disk; }
/* * Get "settable" adapter properties from MegaCLI. */ MRADPPROPS *getMRAdapterProps(int adapter_id) { MRADPPROPS *adp_props = 0; FILE *megacli = NULL; char *command = NULL, *strtok_result = NULL; char line[MAX_MC_LINE] = {0}; int status = 0; adp_props = (MRADPPROPS *) calloc(1, sizeof(MRADPPROPS)); if (adp_props != NULL) { adp_props->adapter_id = adapter_id; /* Get CacheFlushInterval */ SAFE_ASPRINTF(&command, "%s -AdpGetProp CacheFlushInterval -a%d -NoLog 2>&1", MEGACLI_BIN, adapter_id); megacli = popen(command, "r"); while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "Cache Flush Interval")) { strtok_result = strtok(line, "="); strtok_result = strtok(NULL, "="); sscanf(strStrip(strtok_result), "%d", &adp_props->cache_flush); } } status = pclose(megacli); FREE_NULL(command); if (status != 0) { return NULL; } /* Get RebuildRate */ SAFE_ASPRINTF(&command, "%s -AdpGetProp RebuildRate -a%d -NoLog 2>&1", MEGACLI_BIN, adapter_id); megacli = popen(command, "r"); while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "Rebuild Rate")) { strtok_result = strtok(line, "="); strtok_result = strtok(NULL, "="); sscanf(strStrip(strtok_result), "%d", &adp_props->rebuild_rate); } } status = pclose(megacli); FREE_NULL(command); if (status != 0) { return NULL; } /* Get ClusterEnable */ SAFE_ASPRINTF(&command, "%s -AdpGetProp ClusterEnable -a%d -NoLog 2>&1", MEGACLI_BIN, adapter_id); megacli = popen(command, "r"); while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "Cluster :")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); strtok_result = strtok(NULL, ":"); if (strcmp(strStrip(strtok_result), "Enabled") == 0) { adp_props->cluster = TRUE; } else if (strcmp(strStrip(strtok_result), "Disabled") == 0) { adp_props->cluster = FALSE; } } } status = pclose(megacli); FREE_NULL(command); if (status != 0) { return NULL; } /* Get NCQDsply */ SAFE_ASPRINTF(&command, "%s -AdpGetProp NCQDsply -a%d -NoLog 2>&1", MEGACLI_BIN, adapter_id); megacli = popen(command, "r"); while (fgets(line, sizeof(line), megacli) != NULL) { if (strstr(line, "NCQ Status is")) { strtok_result = strtok(line, ":"); strtok_result = strtok(NULL, ":"); if (strcmp(strStrip(strtok_result), "NCQ Status is Enabled") == 0) { adp_props->ncq = TRUE; } else if (strcmp(strStrip(strtok_result), "NCQ Status is Disabled") == 0) { adp_props->ncq = FALSE; } } } status = pclose(megacli); FREE_NULL(command); if (status != 0) { FREE_NULL(adp_props); return NULL; } } /* Done */ return adp_props; }