Esempio n. 1
0
File: pdh.c Progetto: 40a/sigar
JNIEXPORT jstring SIGAR_JNI(win32_Pdh_pdhGetDescription)
(JNIEnv *env, jclass cur, jlong counter)
{
    HCOUNTER h_counter = (HCOUNTER)counter;
    PDH_COUNTER_INFO *info = NULL;
    jstring retval = NULL;
    DWORD size = 0;
    PDH_STATUS status;

    status = PdhGetCounterInfo(h_counter, TRUE, &size, NULL);
    if (status != PDH_MORE_DATA) {
        win32_throw_exception(env, get_error_message(status));
        return NULL;
    }

    info = malloc(size);

    status = PdhGetCounterInfo(h_counter, 1, &size, info);
    if (status == ERROR_SUCCESS) {
        if (info->szExplainText) {
            retval = JENV->NewString(env, info->szExplainText,
                                     lstrlen(info->szExplainText));
        }
    }
    else {
        win32_throw_exception(env, get_error_message(status));
    }

    free(info);
    return retval;
}
int DiskTimeMuninNodePlugin::GetConfig(char *buffer, int len) 
{  
  if (!m_DiskTimeCounters.empty()) {
    PDH_STATUS status;  
    DWORD infoSize = 0;
    status = PdhGetCounterInfo(m_DiskTimeCounters[0], TRUE, &infoSize, NULL);
    if (status != PDH_MORE_DATA)
      return -1;

    PDH_COUNTER_INFO *info = (PDH_COUNTER_INFO *)malloc(infoSize);
    status = PdhGetCounterInfo(m_DiskTimeCounters[0], TRUE, &infoSize, info);
    if (status != ERROR_SUCCESS)
      return -1;

    int printCount;
    printCount = _snprintf(buffer, len, "graph_title Disk Time\n"
      "graph_category system\n"
      "graph_args --base 1000 -l 0\n"
      "graph_info %s\n"
      "graph_vlabel %s\n", info->szExplainText, info->szCounterName);
    len -= printCount;
    buffer += printCount;

    free(info);

    assert(m_DiskTimeNames.size() == m_DiskTimeCounters.size());
    for (size_t i = 0; i < m_DiskTimeNames.size(); i++) {
      printCount = _snprintf(buffer, len, "disktime_%i_.label %s\n", i, m_DiskTimeNames[i].c_str());
      len -= printCount;
      buffer += printCount;
    }
  }

  strncat(buffer, ".\n", len);
  return 0;
}
Esempio n. 3
0
File: pdh.c Progetto: 40a/sigar
JNIEXPORT jlong SIGAR_JNI(win32_Pdh_pdhGetCounterType)
(JNIEnv *env, jclass cur, jlong counter)
{
    HCOUNTER h_counter = (HCOUNTER)counter;
    PDH_COUNTER_INFO info;
    DWORD size = sizeof(info);
    PDH_STATUS status;

    status = PdhGetCounterInfo(h_counter, FALSE, &size, &info);
    if (status != ERROR_SUCCESS) {
        win32_throw_exception(env, get_error_message(status));
        return -1;
    }

    return info.dwType;
}
int PerfCounterMuninNodePlugin::GetConfig(char *buffer, int len) 
{  
  if (!m_Counters.empty()) {
    PDH_STATUS status;  
    DWORD infoSize = 0;
    status = PdhGetCounterInfo(m_Counters[0], TRUE, &infoSize, NULL);
    if (status != PDH_MORE_DATA)
      return -1;

    PDH_COUNTER_INFO *info = (PDH_COUNTER_INFO *)malloc(infoSize);
    status = PdhGetCounterInfo(m_Counters[0], TRUE, &infoSize, info);
    if (status != ERROR_SUCCESS)
      return -1;

    int printCount;
    std::string graphTitle = g_Config.GetValue(m_SectionName, "GraphTitle", "Disk Time");
    std::string graphCategory = g_Config.GetValue(m_SectionName, "GraphCategory", "system");
    std::string graphArgs = g_Config.GetValue(m_SectionName, "GraphArgs", "--base 1000 -l 0");
    std::string explainText = info->szExplainText ? W2AConvert(info->szExplainText) : m_CounterNames[0].c_str();
	std::string counterName = W2AConvert(info->szCounterName);
    printCount = _snprintf(buffer, len, "graph_title %s\n"
      "graph_category %s\n"
      "graph_args %s\n"
      "graph_info %s\n"
      "graph_vlabel %s\n", 
      graphTitle.c_str(), graphCategory.c_str(), 
      graphArgs.c_str(),
	  explainText.c_str(), counterName.c_str());
    len -= printCount;
    buffer += printCount;

    free(info);

    std::string graphDraw = g_Config.GetValue(m_SectionName, "GraphDraw", "LINE");
	std::string counterType = g_Config.GetValue(m_SectionName, "CounterType", "GAUGE");

	std::string minValue;
	std::string minValueNumbered;

	if(counterType == "DERIVE") {
		minValue = "%s.min 0\n";
		minValueNumbered = "%s_%i_.min 0\n";
	} else {
		minValue = minValueNumbered = "";
	}

    assert(m_CounterNames.size() == m_Counters.size());

	std::string labels;

      // We handle multiple counters
      for (size_t i = 0; i < m_CounterNames.size(); i++) {
        if (i == 0) {

		  labels = "%s.label %s\n"
				   "%s.draw %s\n"
				   "%s.type %s\n";
		  labels += minValue;

          // First counter gets a normal name
          printCount = _snprintf(buffer, len, 
			labels.c_str(),
            m_Name.c_str(), m_CounterNames[i].c_str(),
            m_Name.c_str(), graphDraw.c_str(),
			m_Name.c_str(), counterType.c_str(),
			m_Name.c_str());
        } else {
          // Rest of the counters are numbered

		  labels = "%s_%i_.label %s\n"
 				   "%s_%i_.draw %s\n"
				   "%s_%i_.type %s\n";
		  labels += minValueNumbered;

          printCount = _snprintf(buffer, len, 
            labels.c_str(),
            m_Name.c_str(), i, m_CounterNames[i].c_str(),
            m_Name.c_str(), i, graphDraw.c_str(),
			m_Name.c_str(), i, counterType.c_str(),
			m_Name.c_str(), i);
        }
        len -= printCount;
        buffer += printCount;
      }
    }

  strncat(buffer, ".\n", len);
  return 0;
}
int PerfCounterMuninNodePlugin::GetConfig(char *buffer, int len) 
{  
	if (!m_Counters.empty()) {

		PDH_STATUS status;
		DWORD infoSize = 0;
		status = PdhGetCounterInfo(m_Counters[0], TRUE, &infoSize, NULL);
		if (status != PDH_MORE_DATA && status != PDH_INSUFFICIENT_BUFFER)
			return -1;

		PDH_COUNTER_INFO *info = (PDH_COUNTER_INFO *)malloc(infoSize);
		status = PdhGetCounterInfo(m_Counters[0], TRUE, &infoSize, info);
		if (status != ERROR_SUCCESS)
			return -1;

		int printCount;
		std::string graphTitle = g_Config.GetValue(m_SectionName, "GraphTitle", "Disk Time");
		std::string graphCategory = g_Config.GetValue(m_SectionName, "GraphCategory", "system");
		std::string graphArgs = g_Config.GetValue(m_SectionName, "GraphArgs", "--base 1000 -l 0");
		std::string explainText = g_Config.GetValue(m_SectionName, "GraphInfo",
			info->szExplainText ? W2IConvert(info->szExplainText) : m_CounterNames[0].c_str());
		std::string counterName = g_Config.GetValue(m_SectionName, "GraphVLabel", W2IConvert(info->szCounterName));
		printCount = _snprintf(buffer, len, "graph_title %s\n"
			"graph_category %s\n"
			"graph_args %s\n"
			"graph_info %s\n"
			"graph_vlabel %s\n",
			graphTitle.c_str(), graphCategory.c_str(),
			graphArgs.c_str(),
			explainText.c_str(), counterName.c_str());
		len -= printCount;
		buffer += printCount;

		free(info);

		std::string graphDraw = g_Config.GetValue(m_SectionName, "GraphDraw", "LINE");
		std::string counterType = g_Config.GetValue(m_SectionName, "CounterType", "GAUGE");
		std::string warning = g_Config.GetValue(m_SectionName, "WarningValue", "");
		std::string critical = g_Config.GetValue(m_SectionName, "CriticalValue", "");

		assert(m_CounterNames.size() == m_Counters.size());

		std::string labels;

		// We handle multiple counters
		for (size_t i = 0; i < m_CounterNames.size(); i++) {
			PDH_STATUS status;
			DWORD infoSize = 0;
			status = PdhGetCounterInfo(m_Counters[i], TRUE, &infoSize, NULL);
			if (status != PDH_MORE_DATA && status != PDH_INSUFFICIENT_BUFFER)
				return -1;

			PDH_COUNTER_INFO *info = (PDH_COUNTER_INFO *)malloc(infoSize);
			status = PdhGetCounterInfo(m_Counters[i], TRUE, &infoSize, info);
			if (status != ERROR_SUCCESS) {
				free(info);
				return -1;
			}

			_Module.LogEvent("PerfCounter plugin: %s: counter id %i", m_Name.c_str(), info->dwType);

			DWORD npl = 260;
			TCHAR *np = new TCHAR[npl];
			PdhMakeCounterPath(&(info->CounterPath), np, &npl, 0);
			_Module.LogEvent("PerfCounter plugin: %s: counter path %ls, %ls", m_Name.c_str(), np, info->CounterPath.szObjectName);
			npl = 260;
			PdhMakeCounterPath(&(info->CounterPath), np, &npl, PDH_PATH_WBEM_INPUT);
			_Module.LogEvent("PerfCounter plugin: %s: counter path %ls, %ls", m_Name.c_str(), np, info->CounterPath.szObjectName);

			std::string explainText = info->szExplainText ? W2IConvert(info->szExplainText) : m_CounterNames[i].c_str();
			char* m_finalName = new char[128];

			if (i == 0)
				_snprintf(m_finalName, 127, "%s", m_Name.c_str());
			else
				_snprintf(m_finalName, 127, "%s_%i_", m_Name.c_str(), i);

			labels = "%s.label %s\n"
				"%s.draw %s\n"
				"%s.type %s\n"
				"%s.info %s\n";

			// First counter gets a normal name
			printCount = _snprintf(buffer, len,
				labels.c_str(),
				m_finalName, m_CounterNames[i].c_str(),
				m_finalName, graphDraw.c_str(),
				m_finalName, counterType.c_str(),
				m_finalName, explainText.c_str());
			len -= printCount;
			buffer += printCount;

			if (counterType == "DERIVE") {
				printCount = _snprintf(buffer, len,
					"%s.min 0\n",
					m_finalName);
				len -= printCount;
				buffer += printCount;
			}

			if (!warning.empty()) {
				printCount = _snprintf(buffer, len,
					"%s.warning %s\n",
					m_finalName,
					warning.c_str());
				len -= printCount;
				buffer += printCount;
			}
			if (!critical.empty()) {
				printCount = _snprintf(buffer, len,
					"%s.critical %s\n",
					m_finalName,
					critical.c_str());
				len -= printCount;
				buffer += printCount;
			}

			free(info);
		}
	}

  strncat(buffer, ".\n", len);
  return 0;
}