JD_IMETHODIMP
JavaPluginFactory5::StartupJVM(JVMInitArgs *initargs) {
    trace("JavaPluginFactory5:StartupJVM\n");

    JDresult ret = JD_OK;

    if (is_java_vm_started) {
	plugin_error("StartupJVM is being called twice!\n");
	return JD_OK;
    }

    EnterMonitor("StartupJVM");
    
    /* Make sure someone did not start it up while we were
       waiting for the monitor */
    if (!is_java_vm_started) {
	ret = javaVM->StartJavaVM(initargs->classpathAdditions);

	if (ret == JD_OK) 
	    is_java_vm_started = 1;
	 else
	     plugin_error("Could not start JavaVM!\n");
    } else {
	plugin_error("StartupJVM has already been called.\n");
    }

    ExitMonitor("StartupJVM");

    return ret;

}
JD_IMETHODIMP
JavaPluginFactory5::CreateInstance(ISupports *aOuter,
				   const JDIID & aIID,
				   void **result) {
    trace("JavaPluginFactory5:CreateInstance\n");

    if (result == NULL) {
        plugin_error("NULL result in create instance");
	return JD_ERROR_NULL_POINTER;
    }

    *result = NULL;

    if (aOuter != NULL) {
        plugin_error("NO_AGGREGATION in create instance!");
	return JD_ERROR_NO_AGGREGATION;
    }

    if (! (aIID.Equals(jIPluginInstanceIID) ||
	   aIID.Equals(jISupportsIID)))
	return JD_NOINTERFACE;

    /* Startup the JVM if it is not already running */
    JavaVM5* vm = GetJavaVM();

    /* Create a new instance and refcount it */
    JavaPluginInstance5 * pluginInstance = new JavaPluginInstance5(this);

    *result = (IPluginInstance *) pluginInstance;
    pluginInstance->AddRef();

    UNUSED(vm);

    return JD_OK;
}
Пример #3
0
int mcp_creategame_handler(void *p) {
	mcp_packet_t incoming = *MCP_CAST(p);

	mcp_responsed = TRUE;

	dword status = net_get_data(incoming.data, 6, dword);
	
	switch (status) {

	case 0x00: {
		plugin_print("mcp game", "successfully created game\n");

		game_created = TRUE;
		break;
	}
	
	case 0x1e: {
		plugin_error("mcp game", "error: invalid game name\n");
		break;
	}

	case 0x1f: {
		plugin_error("mcp game", "error: game already exists\n");
		// remove this (only for testing purposes)
		//game_created = TRUE;
		break;
	}

	case 0x20: {
		plugin_error("mcp game", "error: game server is unavailable\n");
		break;
	}

	case 0x6e: {
		plugin_error("mcp game", "error: a dead hardcore character cannot create games\n");
		break;
	}

	}

	pthread_mutex_lock(&game_created_mutex);

	pthread_cond_signal(&game_created_cond_v);

	pthread_mutex_unlock(&game_created_mutex);

	return FORWARD_PACKET;
}
ProxySupport5* JavaPluginFactory5::GetProxySupport(void) {
    trace("JavaPluginFactory5:GetProxySupport");
    if (proxy_support == NULL) {
	plugin_error("Proxy support is null!");
    }
    return proxy_support;
}
JD_IMETHODIMP
JavaPluginFactory5::GetJavaWrapper(JNIEnv* proxy_env, 
				   jint browser_obj, 
				   jobject* java_obj) {
    trace("JavaPluginFactory5:JavaPluginFactory5::GetJavaWrapper()\n");

    /* Can determine the right RemoteJNIEnv from looking up the
       env for the current thread, or can look up the remote
       JNIEnvs to find out which one corresponds to this proxyenv */

    if (browser_obj == 0 || java_obj == NULL)    
	return JD_ERROR_NULL_POINTER;
    
    RemoteJNIEnv* env = GetRemoteEnv(proxy_env);
    env->ExceptionClear();
    jclass jsobj5_clazz = 
    env->FindClass("sun/plugin/javascript/navig5/JSObject");

    if (jsobj5_clazz == NULL) 
      plugin_error("Could not create the java wrapper. No JSObject\n");

    jmethodID jsobj_create_method = env->GetMethodID(jsobj5_clazz, 
						     "<init>",
						     "(II)V");
    *java_obj = env->NewObject(jsobj5_clazz, jsobj_create_method,
			      (jint)(g_unixService->JD_GetCurrentThread()), (jint) browser_obj);
    return JD_OK;
}
JD_METHOD JavaPluginFactory5::Create(ISupports* sm,   
				     const JDIID& aIID,    
				     void* *aInstancePtr) {

    JDresult rv = JD_ERROR_FAILURE;
    if (aInstancePtr == NULL) {
        plugin_error("Received a null pointer to pointer in Create!\n");
    } else {
        JavaPluginFactory5 *res;

        if (!g_plugin_factory) {
	    IPluginServiceProvider* spService;
	    if (JD_FAILED(sm->QueryInterface(jIPluginServiceProviderIID, (void**)&spService)))
	        return rv;
	    
            res = new JavaPluginFactory5(spService); // g_plugin_factory set in here
            // The peice of shit browser does not seem to call Initialize
            // on this code path!!!
            spService->Release();
	    res->Initialize(); 
            init_utils();
        } else {
            res = g_plugin_factory;
        }
        rv = res->QueryInterface(aIID,aInstancePtr);
    }
    return rv;
}
JavaPluginInstance5 *JavaPluginFactory5::GetInstance(int index) {
    trace("JavaPluginFactory5:GetInstance\n");

    JavaPluginInstance5* res;
    EnterMonitor("GetInstance");
    
    /* First handle the -1 index - pick a random instance */
    if (index == -1) {
	for (int i = 0; i < PLUGIN_INSTANCE_COUNT; i++) {
	    if (plugin_instances[i] != (JavaPluginInstance5 *)NULL) {
		if (tracing)
		    trace("JavaPluginFactory5::Chose random instance %d\n", i);
		ExitMonitor("GetInstance-any");
		return plugin_instances[i];
	    }
	}
        trace("JavaPluginFactory5:Returning NULL for random instance");
        return (JavaPluginInstance5 *)NULL; 
    }

    /* For a non-random index, check bounds */
    if ((index < 0) || (index >= PLUGIN_INSTANCE_COUNT)) {
	plugin_error("Plugin instance index out of bounds %d\n", index);
	res =  (JavaPluginInstance5 *)NULL;
    } else {
	res = (JavaPluginInstance5 *) plugin_instances[index];    
	if (res == NULL) 
	    trace("JavaPluginFactory::CreateInstance Returning a NULL instance! %d\n", index);
    }
    ExitMonitor("GetInstance-normal");

    return res;
}
JD_IMETHODIMP
JavaPluginFactory5::Initialize() {
  // With the new API, the service manager
  // made available in the GetFactory method and is 
  // then passed into the constructor of the factory,
  // where the CPluginServiceProvider is created.
  // No args to initialize
  
    static JDresult error = JD_ERROR_FAILURE;
    if (isInitialized) 
        return error;
    else 
        isInitialized = true;
    trace("JavaPluginFactory5::Initialize\n");

    /* Set the plugin and jvm managers */
    //XXXFIX consider the lifetime of these objects and when
    // the refs should be released. They should probably
    // be freed when this object is destroyed, but there might
    // be circular references from the plugin manager to the
    // plugin factory and back.
    if (JD_FAILED(service_provider->QueryService(jCPluginManagerCID, 
						 jIPluginManagerIID, 
						 (ISupports **)&plugin_manager)))
      plugin_error("Could not get the plugin manager");
    
    if (plugin_manager != NULL) {
	/* Dump the environment variables for debugging */
	if (tracing) {
	    char *cp = getenv("CLASSPATH");
	    char *jtrace = getenv("JAVA_PLUGIN_TRACE");
	    char *vmwait  = getenv("JAVA_VM_WAIT");
	    char *ldpath = getenv("LD_LIBRARY_PATH");
	    if (cp) trace("CLASSPATH = %s\n", cp);
	    if (jtrace) trace("JAVA_PLUGIN_TRACE = %s\n", jtrace);
	    if (vmwait) trace("JAVA_VM_WAIT = %s\n", vmwait);
	    if (ldpath) trace("LD_LIBRARY_PATH = %s\n", ldpath);
	}
	return error;
    } else {
	plugin_error("No manager for initializing factory?\n");
	error = JD_ERROR_ILLEGAL_VALUE;
	return error;
    }

}
Пример #9
0
static void
get_paging_status (bool show_swapping, bool swapping_only,
		   paging_data_t *paging)
{
  struct proc_vmem *vmem = NULL;
  unsigned long nr_vmem_pgpgin[2], nr_vmem_pgpgout[2];
  unsigned long nr_vmem_pgfault[2];
  unsigned long nr_vmem_pgfree[2];
  unsigned long nr_vmem_pgmajfault[2];
  unsigned long nr_vmem_pgsteal[2];
  unsigned long nr_vmem_pgscand[2];
  unsigned long nr_vmem_pgscank[2];
  unsigned long nr_vmem_dpswpin[2], nr_vmem_dpswpout[2];
  unsigned int i, tog = 0, sleep_time = 1;
  int err;

  err = proc_vmem_new (&vmem);
  if (err < 0)
    plugin_error (STATE_UNKNOWN, err, "memory exhausted");

  for (i = 0; i < 2; i++)
    {
      proc_vmem_read (vmem);

      nr_vmem_pgpgin[tog] = proc_vmem_get_pgpgin (vmem);
      nr_vmem_pgpgout[tog] = proc_vmem_get_pgpgout (vmem);
      nr_vmem_pgfault[tog] = proc_vmem_get_pgfault (vmem);
      nr_vmem_pgmajfault[tog] = proc_vmem_get_pgmajfault (vmem);
      nr_vmem_pgfree[tog] = proc_vmem_get_pgfree (vmem);
      nr_vmem_pgsteal[tog] = proc_vmem_get_pgsteal (vmem);
      nr_vmem_pgscand[tog] = proc_vmem_get_pgscand (vmem);
      nr_vmem_pgscank[tog] = proc_vmem_get_pgscank (vmem);

      nr_vmem_dpswpin[tog] = proc_vmem_get_pswpin (vmem);
      nr_vmem_dpswpout[tog] = proc_vmem_get_pswpout (vmem);

      sleep (sleep_time);
      tog = !tog;
    }

  paging->dpgpgin = nr_vmem_pgpgin[1] - nr_vmem_pgpgin[0];
  paging->dpgpgout = nr_vmem_pgpgout[1] - nr_vmem_pgpgout[0];
  paging->dpgfault = nr_vmem_pgfault[1] - nr_vmem_pgfault[0];
  paging->dpgmajfault = nr_vmem_pgmajfault[1] - nr_vmem_pgmajfault[0];
  paging->dpgfree = nr_vmem_pgfree[1] - nr_vmem_pgfree[0];
  paging->dpgsteal = nr_vmem_pgsteal[1] - nr_vmem_pgsteal[0];
  paging->dpgscand = nr_vmem_pgscand[1] - nr_vmem_pgscand[0];
  paging->dpgscank = nr_vmem_pgscank[1] - nr_vmem_pgscank[0];

  paging->dpswpin = nr_vmem_dpswpin[1] - nr_vmem_dpswpin[0];
  paging->dpswpout = nr_vmem_dpswpout[1] - nr_vmem_dpswpout[0];

  paging->summary =
    swapping_only ? (paging->dpswpin +
		     paging->dpswpout) : paging->dpgmajfault;

  proc_vmem_unref (vmem);
}
/* The folowing methods are not locked since there should be no race
   between modifying and reading the remote env table i.e.
   Looking up an env by index must always work (or there's a bug)
   Looking up an env by the current thread  
     - if there is no env for the thread, then only the current thread
        can remove it and vice versa.
   */
RemoteJNIEnv*
JavaPluginFactory5::GetRemoteEnv(JNIEnv* proxy_env) {
    for(int i = 0; i < MAX_ENVS; i++) {
	if (current_proxy_envs[i] == proxy_env) {
	  return current_envs[i];
	}
    }
    plugin_error("No remote env found for the proxy_env\n");
    return NULL;
}
JD_METHOD createPluginFactory(ISupports* sm,
			      IUnixService* us,
			      IFactory* *res) {
  trace("JavaPluginFactory5::createPluginFactory:\n");

  if (sm == NULL || res == NULL) {
    plugin_error("NULL pointer received when initializing factory!");
  }

  if (us == NULL) {
    plugin_error("No Unix Service!");
  }

  g_unixService = us;

  return JavaPluginFactory5::Create(sm, 
				   JD_GET_IID(IFactory),
				   (void**)res);
}
/* The interface to CookieSupport to get the Cookie 
   Service from browser
*/
ICookieStorage* 
JavaPluginFactory5::GetCookieStorage(void) {
        if (cookieStorage == NULL) {
            if (JD_FAILED(service_provider->QueryService(jCPluginManagerCID,
    						 jICookieStorageIID,
    						 (ISupports **) &cookieStorage)))
                plugin_error("Could not get the CookieStorage");
        }
	return cookieStorage;
}
void JavaPluginFactory5::SendRequest(const CWriteBuffer& wb, int wait_for_reply)
{
    EnterMonitor("SendRequest");
    if (is_java_vm_started)
	javaVM->SendRequest(wb, wait_for_reply);
    else {
	plugin_error("VM is not yet started up in SendRequest!");
    }
    ExitMonitor("SendRequest");
}
/* Returns JVMManager */
IJVMManager* 
JavaPluginFactory5::GetJVMManager(void) {
        if (jvm_manager == NULL) {
            if (JD_FAILED(service_provider->QueryService(jCJVMManagerCID, 
						         jIJVMManagerIID, 
						         (ISupports **) &jvm_manager)))
                plugin_error("Could not get the JVM manager");

        }
	return jvm_manager;
}
Пример #15
0
struct iflist* netinfo (unsigned int seconds)
{
  struct iflist *iflhead = get_netinfo_snapshot ();

  if (seconds > 0)
   {
      sleep (seconds);
      struct iflist *ifl, *ifl2, *iflhead2 = get_netinfo_snapshot ();

      for (ifl = iflhead, ifl2 = iflhead2; ifl != NULL && ifl2 != NULL;
	   ifl = ifl->next, ifl2 = ifl2->next)
	{
	  dbg ("network interface '%s'\n", ifl->ifname);
	  if (STRNEQ (ifl->ifname, ifl2->ifname))
	    plugin_error (STATE_UNKNOWN, 0,
			  "bug in netinfo(), please contact the developers");

	  dbg ("\ttx_packets : %u %u\n", ifl->tx_packets, ifl2->tx_packets);
	  ifl->tx_packets = (ifl2->tx_packets - ifl->tx_packets) / seconds;

	  dbg ("\trx_packets : %u %u\n", ifl->rx_packets, ifl2->rx_packets);
	  ifl->rx_packets = (ifl2->rx_packets - ifl->rx_packets) / seconds;

	  dbg ("\ttx_bytes   : %u %u\n", ifl->tx_bytes, ifl2->tx_bytes);
	  ifl->tx_bytes   = (ifl2->tx_bytes   - ifl->tx_bytes  ) / seconds;

	  dbg ("\trx_bytes   : %u %u\n", ifl->rx_bytes, ifl2->rx_bytes);
	  ifl->rx_bytes   = (ifl2->rx_bytes   - ifl->rx_bytes  ) / seconds;

	  dbg ("\ttx_errors  : %u %u\n", ifl->tx_errors, ifl2->tx_errors);
	  ifl->tx_errors  = (ifl2->tx_errors  - ifl->tx_errors ) / seconds;

	  dbg ("\trx_errors  : %u %u\n", ifl->rx_errors, ifl2->rx_errors);
	  ifl->rx_errors  = (ifl2->rx_errors  - ifl->rx_errors ) / seconds;

	  ifl->tx_dropped = (ifl2->tx_dropped - ifl->tx_dropped) / seconds;
	  dbg ("\ttx_dropped : %u %u\n", ifl->tx_dropped, ifl2->tx_dropped);

	  dbg ("\trx_dropped : %u %u\n", ifl->rx_dropped, ifl2->rx_dropped);
	  ifl->rx_dropped = (ifl2->rx_dropped - ifl->rx_dropped) / seconds;

	  dbg ("\tmulticast  : %u %u\n", ifl->multicast, ifl2->multicast);
	  ifl->multicast  = (ifl2->multicast  - ifl->multicast ) / seconds;

	  dbg ("\tcollisions : %u %u\n", ifl->collisions, ifl2->collisions);
	  ifl->collisions = (ifl2->collisions - ifl->collisions) / seconds;
	}
      freeiflist (iflhead2);
   }

  return iflhead;
}
Пример #16
0
int
main (int argc, char **argv)
{
  int c, i;
  nagstatus status = STATE_OK;

  set_program_name (argv[0]);

  while ((c = getopt_long (argc, argv, GETOPT_HELP_VERSION_STRING, longopts,
                           NULL)) != -1)
    {
      switch (c)
	{
	default:
	  usage (stderr);

	case_GETOPT_HELP_CHAR
	case_GETOPT_VERSION_CHAR

	}
    }

  if (argc <= optind)
    usage (stderr);

  mount_list = read_file_system_list (false);

  if (NULL == mount_list)
    /* Couldn't read the table of mounted file systems. */
    plugin_error (STATE_UNKNOWN, 0,
                  "cannot read table of mounted file systems");

  for (i = optind; i < argc; ++i)
    if (STATE_CRITICAL == check_entry (argv[i]))
      status = STATE_CRITICAL;
    else
      /* This is allowed. See:
       * http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf */
      argv[i] = NULL;

  printf ("filesystems %s", state_text (status));
  for (i = optind; i < argc; ++i)
    if (argv[i])
      printf (" %s", argv[i]);

  if (STATE_CRITICAL == status)
    printf (" unmounted!");
  putchar ('\n');

  return status;
}
Пример #17
0
/* Counterpart to read fully. 0 on failure */
int
write_PR_fully(const char* msg, PRFileDesc* pr, char* buff, int len) {
    int offset = 0;
    int rc;
    while (offset < len) {
        rc = PR_Write(pr, buff + offset, len - offset);
        if (rc <= 0) {
	    plugin_error("Write failed. %s \n", msg);
	    return 0;
	}
	offset += rc;
    }
    return 1;
}
JavaVM5*
JavaPluginFactory5::GetJavaVM(void) {
  if (!is_java_vm_started) {
    JVMInitArgs args;
    args.version = JVMInitArgs_Version;
    args.classpathAdditions = NULL;
    JDresult ret = StartupJVM(&args);
    if (JD_OK != ret) {
      plugin_error("VM did not start up properly");
      is_java_vm_started = 0;
      return NULL;
    }
  }
  return javaVM;
}
/* Create a new applet, with a particular index */
void
JavaPluginFactory5::CreateApplet(const char* appletType, int appletNumber,
				 int argc, char **argn, char **argv) {
    trace("JavaPluginFactory5:CreateApplet\n");

    /* Not sure if calling create applet is permitted before startup */
    while (!is_java_vm_started) {
	plugin_error("CreateApplet called before the VM is started\n?");
	sleep(1);
    }

    EnterMonitor("CreateApplet");

    /* The VM could have been shutdown again */
    if (is_java_vm_started) {
	trace("JavaPluginFactory5::CreateApplet %d \n", appletNumber);
	javaVM->CreateApplet(appletType, appletNumber, argc, argn, argv);
    } else {
	plugin_error("VM not initialized. Cannot create applet!");
    }

    ExitMonitor("CreateApplet");
	
}
/* Free the index assocaited with the remote env 'env'. Should be called
   when the remote env is destroyed, which should be when the securejni
   is destroyed */
int 
JavaPluginFactory5::UnregisterRemoteEnv(RemoteJNIEnv* env) {
    EnterMonitor("Register Env");
    for(int i = 0; i < MAX_ENVS; i++) {
	if (current_envs[i] == env) {
	    current_envs[i] = NULL;
	    current_proxy_envs[i] = NULL;
	    ExitMonitor("Register Env");
	    return i;
	}
    }
    ExitMonitor("Register Env");
    plugin_error("No such env found!");
    return -1;
}
/* Register an env and return an index associated with that env. 
   This index should not be used directly, but may be useful for 
   debugging purposes. We also associate the current thread with
   'env' */
int 
JavaPluginFactory5::RegisterRemoteEnv(RemoteJNIEnv* env, JNIEnv* proxy_env) {
    EnterMonitor("Register Env");
    for(int i = 0; i < MAX_ENVS; i++) {
	if (current_envs[i] == NULL) {
	    current_envs[i] = env;
	    current_proxy_envs[i] = proxy_env;
	    trace("JavaPluginFactory5: Register Env [%d] proxyenv=%d\n", i, (int) proxy_env);
	    ExitMonitor("Register Env");
	    return i;
	}
    }
    ExitMonitor("Register Env");
    plugin_error("Env table is full!");
    return -1;
}
Пример #22
0
static struct iflist* get_netinfo_snapshot (void)
{
  int family;
  struct ifaddrs *ifaddr, *ifa;

  if (getifaddrs (&ifaddr) == -1)
    plugin_error (STATE_UNKNOWN, errno, "getifaddrs() failed");

  struct iflist *iflhead = NULL, *iflprev = NULL, *ifl;

  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
    {
      if (ifa->ifa_addr == NULL || ifa->ifa_data == NULL)
        continue;

      family = ifa->ifa_addr->sa_family;
      if (family != AF_PACKET)
	continue;

      struct rtnl_link_stats *stats = ifa->ifa_data;
      ifl = xmalloc (sizeof (struct iflist));

      ifl->ifname = xstrdup (ifa->ifa_name);
      ifl->tx_packets = stats->tx_packets;
      ifl->rx_packets = stats->rx_packets;
      ifl->tx_bytes   = stats->tx_bytes; 
      ifl->rx_bytes   = stats->rx_bytes;
      ifl->tx_errors  = stats->tx_errors;
      ifl->rx_errors  = stats->rx_errors;
      ifl->tx_dropped = stats->tx_dropped;
      ifl->rx_dropped = stats->rx_dropped;
      ifl->multicast  = stats->multicast;
      ifl->collisions = stats->collisions;
      ifl->next = NULL;

      if (iflhead == NULL)
	iflhead = ifl;
      else
	iflprev->next = ifl;
      iflprev = ifl;
    }

  freeifaddrs (ifaddr);
  return iflhead;
}
Пример #23
0
/* Read length bytes fully from 'pr' into buff, reporting an error
   if reading fails for some reason. 0 on failure */
int
read_PR_fully(const char* msg, PRFileDesc* pr, char* buffer, int length) {
    int cur, res = 0;
    int rem_length = length;
    if (length == 0) return 1;
    while (rem_length > 0) {
	cur = PR_Read(pr, buffer, rem_length);
	if (cur == rem_length) return 1;
	if (cur <= 0) {
	    plugin_error("Pipe closed during read? State may be corrupt");
	    return 0;
	}
	buffer = ((char*) buffer + cur);
	res += cur;
	rem_length -= cur;
    }
    UNUSED(msg);
    return 1;
}
/*  
 * Plugin Internal: Register an instance with the plugin We would like
 * to be able to recover PluginInstances from the plugin.  So we keep
 * a registry of currently running instances. We provide the functions
 * to register, unregister, and get a plugin instance from that
 * registry.
 */
int 
JavaPluginFactory5::RegisterInstance(JavaPluginInstance5* pluginInstance) {
    trace("JavaPluginFactory5:RegisterInstance\n");
    int i;

    EnterMonitor("RegisterInstance");
    for (i = 0; i < PLUGIN_INSTANCE_COUNT; i++) {
	if (plugin_instances[i] == NULL) {	
	    trace("JavaPluginFactory5::RegisterInstance %d at %d\n", 
		  (int) pluginInstance, i);
	    plugin_instances[i] = pluginInstance;
	    ExitMonitor("RegisterInstance");
	    return i;
	}
    }
    plugin_error("Could not register plugininstance\n");
    ExitMonitor("RegisterInstance");
    return JD_ERROR_FAILURE;
}
Пример #25
0
int
main (int argc, char **argv)
{
  int c, n_ports, n_online;
  bool verbose = false, summary = false;
  char *critical = NULL, *warning = NULL;
  nagstatus status = STATE_OK;
  thresholds *my_threshold = NULL;

  unsigned long delay, count;
  unsigned int sleep_time = 1;

  set_program_name (argv[0]);

  while ((c = getopt_long (argc, argv,
			   "c:w:vi" GETOPT_HELP_VERSION_STRING,
			   longopts, NULL)) != -1)
    {
      switch (c)
	{
	default:
	  usage (stderr);
	case 'i':
	  summary = true;
	  break;
	case 'c':
	  critical = optarg;
	  break;
	case 'w':
	  warning = optarg;
	  break;
	case 'v':
	  verbose = true;
	  break;

	case_GETOPT_HELP_CHAR
	case_GETOPT_VERSION_CHAR

	}
    }

  if (summary)
    {
      fc_host_summary (verbose);
      return STATE_UNKNOWN;
    }

  delay = DELAY_DEFAULT, count = COUNT_DEFAULT;
  if (optind < argc)
    {
      delay = strtol_or_err (argv[optind++], "failed to parse argument");

      if (delay < 1)
	plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
      else if (UINT_MAX < delay)
	plugin_error (STATE_UNKNOWN, 0, "too large delay value");

      sleep_time = delay;
    }

  if (optind < argc)
    count = strtol_or_err (argv[optind++], "failed to parse argument");

  status = set_thresholds (&my_threshold, warning, critical);
  if (status == NP_RANGE_UNPARSEABLE)
    usage (stderr);

  fc_host_statistics stats = {0};

  fc_host_status (&n_ports, &n_online, &stats, sleep_time, count);
  status = get_status (n_online, my_threshold);

  printf ("%s %s - Fiber Channel ports status: %d/%d Online "
	  "| rx_frames=%Lu tx_frames=%Lu"
	  " error_frames=%Lu"
	  " invalid_crc_count=%Lu"
	  " link_failure_count=%Lu"
	  " loss_of_signal_count=%Lu"
	  " loss_of_sync_count=%Lu\n",
	  program_name_short, state_text (status),
	  n_online, n_ports,
	  (unsigned long long) stats.rx_frames,
	  (unsigned long long) stats.tx_frames,
	  (unsigned long long) stats.error_frames,
	  (unsigned long long) stats.invalid_crc_count,
	  (unsigned long long) stats.link_failure_count,
	  (unsigned long long) stats.loss_of_signal_count,
	  (unsigned long long) stats.loss_of_sync_count
  );

  return status;
}
Пример #26
0
/* Handle the next message from Java to JS */
void
JSHandler(RemoteJNIEnv* env) {
	/* 2 cases 
	   - if this is recursive then we have sole ownership of the
	   pipe. 
	   - If this is spontaneous, we should also have sole ownership of
	   the pipe since it is locked at the other end 
	   - Hence, there should be no need for locking here
	*/
	/* Get the plugin index */
	int pluginIndex;
	get_msg(env, (char*)&pluginIndex, 4);
	JavaPluginFactory5* plugin_factory = get_global_factory();
	JavaPluginInstance5* inst = plugin_factory->GetInstance(pluginIndex);
	int code;
	get_msg(env, (char*)&code, 4);
  
	/*
	 * when  JSObject is GCed after plugin is destroyed, inst is NULL 
	 * and code is JAVA_PLUGIN_JNIJS_FINALIZE. In this case, we still
	 * need consume message from spontaneous pipe and ask browser to 
	 * release native JSObject,  or spontaneous pipe will be corrupted
	 * and resource leak
	 */
	if (code != JAVA_PLUGIN_JNIJS_FINALIZE) {

		if (inst == NULL || inst->IsDestroyPending()) {

			// Consume the message
			int raw_msg_len;
			char* raw_msg;
			get_msg(env, &raw_msg_len, 4);
			raw_msg = (char *) checked_malloc(raw_msg_len);
			/* Swallow the whole message */
			get_msg(env, raw_msg, raw_msg_len);
			int replyID = 0;
			memcpy(&replyID, raw_msg, 4);
			free(raw_msg);
			jobject nullret = NULL;
			send_jnijsOK_res(env, replyID, &nullret, sizeof(jobject));
			return;
		}
	}
  
	JSMessage msg;
	trace("JSObject:Entering JSHandler()\n");

	UnpackJSMessage(env, &msg);

	// Create ProxyJNIEnv
	JNIEnv* pProxyJNIEnv = NULL;
	IJVMManager* jvm_manager;
	jvm_manager = plugin_factory->GetJVMManager();
    
	ILiveconnect* pLiveConnect = NULL;
	ISecurityContext* pContext = NULL;
    
	if (JD_SUCCEEDED(jvm_manager->CreateProxyJNI(NULL, &pProxyJNIEnv)) ) {
		trace("JSHandler(): JS command: %X %s\n", code, jscode_to_str(code));
	
		if (inst == NULL) {
			IPluginServiceProvider* pProvider = plugin_factory->GetServiceProvider();

			if (pProvider == NULL) {
				trace("JSHandler(): cannot get pProvider when inst is NULL\n");
				return;
			}

			if (JD_FAILED(pProvider->QueryService(jCLiveconnectCID,
					jILiveconnectIID, (ISupports**)&pLiveConnect))) {
				trace("JSHandler(): cannot get liveconnect when inst is NULL\n");
				return;
			}

		} else {
	
			if (JD_FAILED(inst->GetJSDispatcher(&pLiveConnect))) {
				return;
			}
		}

		if (msg.utfstr != NULL)
			CreateSecurityContext(msg.utfstr, (int) msg.ctx, &pContext);

		int replyID = msg.requestID;

		switch(code){
		case JAVA_PLUGIN_JNIJS_GET_NATIVE:
			{
				// CLiveconnect	  
				jsobject ret = 0;

				/* Get the JS object, which represents a window associated
				   with the given plugin instance */

				JDresult nr = pLiveConnect->GetWindow(pProxyJNIEnv, 
													  (IPluginInstance*) inst, 
													  (void **)NULL, 
													  0, 
													  (ISecurityContext*) pContext, 
													  &ret);
				if(JD_FAILED(nr) || ret == 0) {
					trace("JSObject::ILiveConnect::GetWindow FAILED\n");
				}
  
				send_jnijsOK_res(env, replyID, &ret, sizeof(jsobject));
				break;
			}
		case JAVA_PLUGIN_JNIJS_TOSTRING:
			{
				jstring ret = NULL;
	  
				JDresult nr = pLiveConnect->ToString(pProxyJNIEnv,
													 (jsobject) msg.nativeJSObject,
													 &ret);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::ToString FAILED\n");
				}
	  	  	
				send_jnijsOK_res(env, replyID, &ret, sizeof(jstring));
				break;
			}
		case JAVA_PLUGIN_JNIJS_FINALIZE:
			{		 
				jobject dummy = NULL;
				JDresult nr = pLiveConnect->FinalizeJSObject(pProxyJNIEnv, 
															 (jsobject) msg.nativeJSObject);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::FinalizeJSObject() FAILED\n");
				}
		
				send_jnijsOK_res(env, replyID, &dummy, sizeof(jobject));
				break;
			}
		case JAVA_PLUGIN_GET_BROWSER_AUTHINFO: 
			{
				IBrowserAuthenticator* pBrowserAuthenticator;
				jobject ret = NULL;
				if (inst != NULL) {
					IPluginServiceProvider* service_provider =  plugin_factory->GetServiceProvider();

					trace("Handle native call: GetBrowserAuthenticat()");
					if(service_provider != NULL && 
					   JD_SUCCEEDED(service_provider->QueryService(jBrowserAuthenticatorCID, 
																   jBrowserAuthenticatorIID,
																   (ISupports**)&pBrowserAuthenticator))) {                       
						trace("Interface IBrowserAuthenticator found");
						ret = GetBrowserAuthInfo(env, msg.jarr, pBrowserAuthenticator);
						service_provider->ReleaseService(jBrowserAuthenticatorCID, pBrowserAuthenticator);
					} else {
						trace("Interface IBrowserAuthenticator not found");
					}
				}

				send_jnijsOK_res(env, replyID, &ret, sizeof(jobject));
				break;
			}
		case JAVA_PLUGIN_JNIJS_CALL:
			{
				jobject ret = NULL;
				JDresult nr = pLiveConnect->Call(pProxyJNIEnv, 
												 (jsobject)msg.nativeJSObject, 
												 msg.charstr, msg.charstr_len, 
												 msg.jarr,
												 (void**)NULL, 0, 
												 (ISecurityContext*)pContext, &ret);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::Call() FAILED\n");
				}
			
				send_jnijsOK_res(env, replyID, &ret, sizeof(jobject));
				break;
			}
		case JAVA_PLUGIN_JNIJS_EVAL:
			{
				jobject ret = NULL;
				JDresult nr = pLiveConnect->Eval(pProxyJNIEnv, 
												 (jsobject)msg.nativeJSObject,
												 msg.charstr, msg.charstr_len,
												 (void**)NULL, 0,
												 (ISecurityContext*)pContext, &ret);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::Eval() FAILED\n");
				}
		
				send_jnijsOK_res(env, replyID, &ret, sizeof(jobject));
				break;
			}
		case JAVA_PLUGIN_JNIJS_GETMEMBER:
			{
				jobject ret = NULL;
		 
				JDresult nr = pLiveConnect->GetMember(pProxyJNIEnv,
													  (jsobject)msg.nativeJSObject,
													  msg.charstr, msg.charstr_len,
													  (void**)NULL, 0,
													  (ISecurityContext*)pContext, &ret);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::GetMember() FAILED\n");
				}
		
				send_jnijsOK_res(env, replyID, &ret, sizeof(jobject));
				break;
			}
		case JAVA_PLUGIN_JNIJS_SETMEMBER:
			{
				jobject dummy = NULL;
	 
				JDresult nr = pLiveConnect->SetMember(pProxyJNIEnv,
													  (jsobject)msg.nativeJSObject,
													  msg.charstr, msg.charstr_len, 
													  msg.value,
													  (void**)NULL, 0,
													  (ISecurityContext*)pContext);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::SetMember() FAILED\n");
				}
	  
				send_jnijsOK_res(env, replyID, &dummy, sizeof(jobject));
				break;
			}
		case JAVA_PLUGIN_JNIJS_REMOVEMEMBER:
			{
				jobject dummy = NULL;
		 
				JDresult nr = pLiveConnect->RemoveMember(pProxyJNIEnv, 
														 (jsobject)msg.nativeJSObject, 
														 msg.charstr, msg.charstr_len, 
														 (void**)NULL, 0, 
														 (ISecurityContext*)pContext);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::RemoveMember() FAILED\n");
				}
		  
				send_jnijsOK_res(env, replyID, &dummy, sizeof(jobject));
				break;
			}
		case JAVA_PLUGIN_JNIJS_GETSLOT:
			{
				jobject ret = NULL;
	 
				JDresult nr = pLiveConnect->GetSlot(pProxyJNIEnv, 
													(jsobject)msg.nativeJSObject, 
													msg.slotindex,
													(void**)NULL, 0, 
													(ISecurityContext*)pContext, &ret);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::GetSlot() FAILED");
				}
				send_jnijsOK_res(env, replyID, &ret, sizeof(jobject));
				break;
			}
		case JAVA_PLUGIN_JNIJS_SETSLOT:
			{
				jobject dummy = NULL;
	  
				JDresult nr = pLiveConnect->SetSlot(pProxyJNIEnv, 
													(jsobject)msg.nativeJSObject, 
													msg.slotindex, 
													msg.value,
													(void**)NULL, 0, 
													(ISecurityContext*)pContext);
				if(!JD_SUCCEEDED(nr)) {
					trace("JSObject::ILiveConnect::SetSlot() FAILED\n");
				}
				send_jnijsOK_res(env, replyID, &dummy, sizeof(jobject));
				break;
			}
		default:
			plugin_error("Error in handler for JS calls!\n");
	
			if (pContext != NULL)
				pContext->Release();
			/* End of the handler for JS method calls */
		}
	}
	else {
		trace("Can not get ProxyJNI\n");
	}
	if(pLiveConnect != NULL)
		pLiveConnect->Release();

	if (pContext)
		pContext->Release();

	FreeJSMessage(&msg);
}
Пример #27
0
int
main (int argc, char **argv)
{
  bool show_swapping = false;
  int c, status, err;
  char *critical = NULL, *warning = NULL;
  char *status_msg;
  char *perfdata_paging_msg, *perfdata_swapping_msg = NULL;
  unsigned int i, tog = 0, sleep_time = 1;
  thresholds *my_threshold = NULL;

  struct proc_vmem *vmem = NULL;
  unsigned long dpgpgin, dpgpgout, dpgfault, dpgfree, dpgmajfault,
    dpgscand, dpgscank, dpgsteal;
  unsigned long nr_vmem_pgpgin[2], nr_vmem_pgpgout[2];
  unsigned long nr_vmem_pgfault[2];
  unsigned long nr_vmem_pgfree[2];
  unsigned long nr_vmem_pgmajfault[2];
  unsigned long nr_vmem_pgsteal[2];
  unsigned long nr_vmem_pgscand[2];
  unsigned long nr_vmem_pgscank[2];
  unsigned long dpswpin, dpswpout;
  unsigned long nr_vmem_dpswpin[2], nr_vmem_dpswpout[2];
  unsigned long *tracked_value = &dpgmajfault;

  set_program_name (argv[0]);

  while ((c = getopt_long (argc, argv, "psc:w:bkmg" GETOPT_HELP_VERSION_STRING,
			   longopts, NULL)) != -1)
    {
      switch (c)
	{
	default:
	  usage (stderr);
	case 'p':
	  /* show_paging = true; */
	  break;
	case 's':
	  show_swapping = true;
	  break;
	case 'c':
	  critical = optarg;
	  break;
	case 'w':
	  warning = optarg;
	  break;

	case_GETOPT_HELP_CHAR
	case_GETOPT_VERSION_CHAR

	}
    }

  status = set_thresholds (&my_threshold, warning, critical);
  if (status == NP_RANGE_UNPARSEABLE)
    usage (stderr);

  err = proc_vmem_new (&vmem);
  if (err < 0)
    plugin_error (STATE_UNKNOWN, err, "memory exhausted");

  for (i = 0; i < 2; i++)
    {
      proc_vmem_read (vmem);

      nr_vmem_pgpgin[tog] = proc_vmem_get_pgpgin (vmem);
      nr_vmem_pgpgout[tog] = proc_vmem_get_pgpgout (vmem);
      nr_vmem_pgfault[tog] = proc_vmem_get_pgfault (vmem);
      nr_vmem_pgmajfault[tog] = proc_vmem_get_pgmajfault (vmem);
      nr_vmem_pgfree[tog] = proc_vmem_get_pgfree (vmem);
      nr_vmem_pgsteal[tog] = proc_vmem_get_pgsteal (vmem);

      if (show_swapping)
	{
	  nr_vmem_dpswpin[tog] = proc_vmem_get_pswpin (vmem);
	  nr_vmem_dpswpout[tog] = proc_vmem_get_pswpout (vmem);
	}

      nr_vmem_pgscand[tog] = proc_vmem_get_pgscand (vmem);
      nr_vmem_pgscank[tog] = proc_vmem_get_pgscank (vmem);

      sleep (sleep_time);
      tog = !tog;
    }

  dpgpgin = nr_vmem_pgpgin[1] - nr_vmem_pgpgin[0];
  dpgpgout = nr_vmem_pgpgout[1] - nr_vmem_pgpgout[0];
  dpgfault = nr_vmem_pgfault[1] - nr_vmem_pgfault[0];
  dpgmajfault = nr_vmem_pgmajfault[1] - nr_vmem_pgmajfault[0];
  dpgfree = nr_vmem_pgfree[1] - nr_vmem_pgfree[0];
  dpgsteal = nr_vmem_pgsteal[1] - nr_vmem_pgsteal[0];
  dpgscand = nr_vmem_pgscand[1] - nr_vmem_pgscand[0];
  dpgscank = nr_vmem_pgscank[1] - nr_vmem_pgscank[0];

  if (show_swapping)
    {
      dpswpin = nr_vmem_dpswpin[1] - nr_vmem_dpswpin[0];
      dpswpout = nr_vmem_dpswpout[1] - nr_vmem_dpswpout[0];

      perfdata_swapping_msg =
	xasprintf (" vmem_pswpin/s=%lu vmem_pswpout/s=%lu", dpswpin,
		   dpswpout);
    }

  proc_vmem_unref (vmem);

  status = get_status (*tracked_value, my_threshold);
  free (my_threshold);

  status_msg =
    xasprintf ("%s: %lu majfault/s", state_text (status), *tracked_value);
  perfdata_paging_msg =
    xasprintf ("vmem_pgpgin/s=%lu vmem_pgpgout/s=%lu vmem_pgfault/s=%lu "
	       "vmem_pgmajfault/s=%lu vmem_pgfree/s=%lu vmem_pgsteal/s=%lu "
	       "vmem_pgscand/s=%lu vmem_pgscank/s=%lu",
	       dpgpgin, dpgpgout, dpgfault, dpgmajfault, dpgfree, dpgsteal,
	       dpgscand, dpgscank);

  printf ("%s %s | %s%s\n", "PAGING", status_msg, perfdata_paging_msg,
	  perfdata_swapping_msg ? perfdata_swapping_msg : "");

  return status;
}
Пример #28
0
int
main (int argc, char **argv)
{
  int c, err;
  bool verbose, cpu_model, per_cpu_stats;
  unsigned long len, i, count, delay;
  char *critical = NULL, *warning = NULL;
  char *p = NULL, *cpu_progname;
  nagstatus currstatus, status;
  thresholds *my_threshold = NULL;

  float cpu_perc = 0.0;
  unsigned int tog = 0;		/* toggle switch for cleaner code */
  struct cpu_desc *cpudesc = NULL;

  set_program_name (argv[0]);

  len = strlen (program_name);
  if (len > 6 && !strncmp (program_name, "check_", 6))
    p = (char *) program_name + 6;
  else
    plugin_error (STATE_UNKNOWN, 0,
		  "bug: the plugin does not have a standard name");

  if (!strncmp (p, "iowait", 6))	/* check_iowait --> cpu_iowait */
    {
      cpu_progname = xstrdup ("iowait");
      program_shorthelp =
        xstrdup ("This plugin checks I/O wait bottlenecks\n");
    }
  else				/* check_cpu --> cpu_user (the default) */
    {
      cpu_progname = xstrdup ("user");;
      program_shorthelp =
        xstrdup ("This plugin checks the CPU (user mode) utilization\n");
    }

  err = cpu_desc_new (&cpudesc);
  if (err < 0)
    plugin_error (STATE_UNKNOWN, err, "memory exhausted");

  /* default values */
  verbose = per_cpu_stats = false;
  cpu_model = true;

  while ((c = getopt_long (
		argc, argv, "c:w:vifmp"
		GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1)
    {
      switch (c)
	{
	default:
	  usage (stderr);
	case 'i':
	  cpu_desc_read (cpudesc);
	  cpu_desc_summary (cpudesc);
	  return STATE_UNKNOWN;
	case 'm':
	  cpu_model = false;
	  break;
	case 'p':
	  per_cpu_stats = true;
	  break;
	case 'c':
	  critical = optarg;
	  break;
	case 'w':
	  warning = optarg;
	  break;
	case 'v':
	  verbose = true;
	  break;

	case_GETOPT_HELP_CHAR
	case_GETOPT_VERSION_CHAR

	}
    }

  delay = DELAY_DEFAULT, count = COUNT_DEFAULT;
  if (optind < argc)
    {
      delay = strtol_or_err (argv[optind++], "failed to parse argument");

      if (delay < 1)
	plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
      else if (DELAY_MAX < delay)
	plugin_error (STATE_UNKNOWN, 0,
		      "too large delay value (greater than %d)", DELAY_MAX);
    }

  if (optind < argc)
    {
      count = strtol_or_err (argv[optind++], "failed to parse argument");
      if (COUNT_MAX < count)
	plugin_error (STATE_UNKNOWN, 0,
		      "too large count value (greater than %d)", COUNT_MAX);
    }

  status = set_thresholds (&my_threshold, warning, critical);
  if (status == NP_RANGE_UNPARSEABLE)
    usage (stderr);

  int ncpus = per_cpu_stats ? get_processor_number_total () + 1 : 1;

  jiff duser[ncpus], dsystem[ncpus], didle[ncpus],
       diowait[ncpus], dsteal[ncpus], ratio[ncpus];
  int debt[ncpus];			/* handle idle ticks running backwards */
  struct cpu_time cpuv[2][ncpus];
  jiff *cpu_value = strncmp (p, "iowait", 6) ? duser : diowait;
  const char *cpuname;

  cpu_stats_get_time (cpuv[0], ncpus);

  for (c = 0; c < ncpus; c++)
    {
      duser[c]   = cpuv[0][c].user + cpuv[0][c].nice;
      dsystem[c] = cpuv[0][c].system + cpuv[0][c].irq + cpuv[0][c].softirq;
      didle[c]   = cpuv[0][c].idle;
      diowait[c] = cpuv[0][c].iowait;
      dsteal[c]  = cpuv[0][c].steal;

      debt[c] = 0;
      ratio[c] = duser[c] + dsystem[c] + didle[c] + diowait[c] + dsteal[c];
      if (!ratio[c])
	ratio[c] = 1, didle[c] = 1;
    }

  for (i = 1; i < count; i++)
    {
      sleep (delay);
      tog = !tog;
      cpu_stats_get_time (cpuv[tog], ncpus);

      for (c = 0; c < ncpus; c++)
	{
	  duser[c] =
	    cpuv[tog][c].user - cpuv[!tog][c].user +
	    cpuv[tog][c].nice - cpuv[!tog][c].nice;
	  dsystem[c] =
	    cpuv[tog][c].system  - cpuv[!tog][c].system +
	    cpuv[tog][c].irq     - cpuv[!tog][c].irq +
	    cpuv[tog][c].softirq - cpuv[!tog][c].softirq;
	  didle[c]   = cpuv[tog][c].idle   - cpuv[!tog][c].idle;
	  diowait[c] = cpuv[tog][c].iowait - cpuv[!tog][c].iowait;
	  dsteal[c]  = cpuv[tog][c].steal  - cpuv[!tog][c].steal;

	  /* idle can run backwards for a moment -- kernel "feature" */
	  if (debt[c])
	    {
	      didle[c] = (int) didle[c] + debt[c];
	      debt[c] = 0;
	    }
	  if ((int) didle[c] < 0)
	    {
	      debt[c] = (int) didle[c];
	      didle[c] = 0;
	    }

	  ratio[c] = duser[c] + dsystem[c] + didle[c] + diowait[c] + dsteal[c];
	  if (!ratio[c])
	    ratio[c] = 1, didle[c] = 1;

	  if (NULL == (cpuname = cpuv[0][c].cpuname))
	    cpuname = "n/a";

	  if (verbose)
	    printf
	     ("%s_user=%.1f%%, %s_system=%.1f%%, %s_idle=%.1f%%, "
	      "%s_iowait=%.1f%%, %s_steal=%.1f%%\n"
	      , cpuname, 100.0 * duser[c]   / ratio[c]
	      , cpuname, 100.0 * dsystem[c] / ratio[c]
	      , cpuname, 100.0 * didle[c]   / ratio[c]
	      , cpuname, 100.0 * diowait[c] / ratio[c]
	      , cpuname, 100.0 * dsteal[c]  / ratio[c]);

	  dbg ("sum (%s_*) = %.1f%%\n", cpuname, (100.0 * duser[c] / ratio[c]) +
	       (100.0 * dsystem[c] / ratio[c]) + (100.0 * didle[c]  / ratio[c]) +
	       (100.0 * diowait[c] / ratio[c]) + (100.0 * dsteal[c] / ratio[c]));
	}
    }

  for (c = 0, status = STATE_OK; c < ncpus; c++)
    {
      cpu_perc = (100.0 * (cpu_value[c]) / ratio[c]);
      currstatus = get_status (cpu_perc, my_threshold);
      if (currstatus > status)
	status = currstatus;
    }

  cpu_desc_read (cpudesc);
  char *cpu_model_str =
    cpu_model ?	xasprintf ("(%s) ",
			   cpu_desc_get_model_name (cpudesc)) : NULL;

  printf ("%s %s%s - cpu %s %.1f%% |"
	  , program_name_short, cpu_model ? cpu_model_str : ""
	  , state_text (status), cpu_progname, cpu_perc);
  for (c = 0; c < ncpus; c++)
    {
      if ((cpuname = cpuv[0][c].cpuname))
        printf (" %s_user=%.1f%% %s_system=%.1f%% %s_idle=%.1f%%"
		" %s_iowait=%.1f%% %s_steal=%.1f%%"
		, cpuname, 100.0 * duser[c]   / ratio[c]
		, cpuname, 100.0 * dsystem[c] / ratio[c]
		, cpuname, 100.0 * didle[c]   / ratio[c]
		, cpuname, 100.0 * diowait[c] / ratio[c]
		, cpuname, 100.0 * dsteal[c]  / ratio[c]);
    }
  putchar ('\n');

  cpu_desc_unref (cpudesc);
  return status;
}
Пример #29
0
int
main (int argc, char **argv)
{
  int c;
  bool verbose = false;
  char *critical = NULL, *warning = NULL;
  nagstatus status = STATE_OK;
  thresholds *my_threshold = NULL;

  unsigned long long nintr[2];
  unsigned int sleep_time = 1,
	   tog = 0,		/* toggle switch for cleaner code */
	   ncpus0, ncpus1;
  unsigned long i, delay, count,
	   *vintr[2] = { NULL, NULL };

  set_program_name (argv[0]);

  while ((c = getopt_long (argc, argv,
			   "c:w:v" GETOPT_HELP_VERSION_STRING,
			   longopts, NULL)) != -1)
    {
      switch (c)
	{
	default:
	  usage (stderr);
	case 'c':
	  critical = optarg;
	  break;
	case 'w':
	  warning = optarg;
	  break;
	case 'v':
	  verbose = true;
	  break;

	case_GETOPT_HELP_CHAR
	case_GETOPT_VERSION_CHAR

	}
    }

  delay = DELAY_DEFAULT, count = COUNT_DEFAULT;
  if (optind < argc)
    {
      delay = strtol_or_err (argv[optind++], "failed to parse argument");

      if (delay < 1)
	plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
      else if (UINT_MAX < delay)
	plugin_error (STATE_UNKNOWN, 0, "too large delay value");

      sleep_time = delay;
    }

  if (optind < argc)
    count = strtol_or_err (argv[optind++], "failed to parse argument");

  status = set_thresholds (&my_threshold, warning, critical);
  if (status == NP_RANGE_UNPARSEABLE)
    usage (stderr);

  unsigned long long dnintr = nintr[0] = cpu_stats_get_intr ();
  if (verbose)
    printf ("intr = %Lu\n", dnintr);

  if (count <= 2)
    vintr[0] = proc_interrupts_get_nintr_per_cpu (&ncpus0);

  for (i = 1; i < count; i++)
    {
      sleep (sleep_time);

      tog = !tog;
      nintr[tog] = cpu_stats_get_intr ();

      dnintr = (nintr[tog] - nintr[!tog]) / sleep_time;
      if (verbose)
	printf ("intr = %Lu --> %Lu/s\n", nintr[tog], dnintr);

      if (count - 2 == i)
	vintr[0] = proc_interrupts_get_nintr_per_cpu (&ncpus0);
      else if (count - 1 == i)
	vintr[1] = proc_interrupts_get_nintr_per_cpu (&ncpus1);
    }

  status = get_status (dnintr, my_threshold);
  free (my_threshold);

  char *time_unit = (count > 1) ? "/s" : "";
  printf ("%s %s - number of interrupts%s %Lu | intr%s=%Lu",
	  program_name_short, state_text (status),
	  time_unit, dnintr, time_unit, dnintr);

  for (i = 0; i < MIN (ncpus0, ncpus1); i++)
    printf (" intr_cpu%lu%s=%lu", i, time_unit,
	    (count >
	     1) ? (vintr[1][i] - vintr[0][i]) / sleep_time : vintr[0][i]);
  printf ("\n");

  free (vintr[1]);
  free (vintr[0]);

  return status;
}
Пример #30
0
int
main (int argc, char **argv)
{
  int c;
  bool verbose = false;
  char *critical = NULL, *warning = NULL;
  nagstatus status = STATE_OK;
  thresholds *my_threshold = NULL;

  unsigned long long nctxt[2];
  unsigned int sleep_time = 1,
	   tog = 0;		/* toggle switch for cleaner code */
  unsigned long i, delay, count;

  set_program_name (argv[0]);

  while ((c = getopt_long (argc, argv,
			   "c:w:v" GETOPT_HELP_VERSION_STRING,
			   longopts, NULL)) != -1)
    {
      switch (c)
	{
	default:
	  usage (stderr);
	case 'c':
	  critical = optarg;
	  break;
	case 'w':
	  warning = optarg;
	  break;
	case 'v':
	  verbose = true;
	  break;

	case_GETOPT_HELP_CHAR
	case_GETOPT_VERSION_CHAR

	}
    }

  delay = DELAY_DEFAULT, count = COUNT_DEFAULT;
  if (optind < argc)
    {
      delay = strtol_or_err (argv[optind++], "failed to parse argument");

      if (delay < 1)
	plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
      else if (UINT_MAX < delay)
	plugin_error (STATE_UNKNOWN, 0, "too large delay value");

      sleep_time = delay;
    }

  if (optind < argc)
    count = strtol_or_err (argv[optind++], "failed to parse argument");

  status = set_thresholds (&my_threshold, warning, critical);
  if (status == NP_RANGE_UNPARSEABLE)
    usage (stderr);

  unsigned long long dnctxt = nctxt[0] = cpu_stats_get_cswch ();
  if (verbose)
    printf ("ctxt = %Lu\n", dnctxt);

  for (i = 1; i < count; i++)
    {
      sleep (sleep_time);

      tog = !tog;
      nctxt[tog] = cpu_stats_get_cswch ();
      dnctxt = (nctxt[tog] - nctxt[!tog]) / sleep_time;

      if (verbose)
	printf ("ctxt = %Lu --> %Lu/s\n", nctxt[tog], dnctxt);
    }

  status = get_status (dnctxt, my_threshold);
  free (my_threshold);

  char *time_unit = (count > 1) ? "/s" : "";
  printf ("%s %s - number of context switches%s %Lu | cswch%s=%Lu\n",
	  program_name_short, state_text (status),
	  time_unit, dnctxt, time_unit, dnctxt);

  return status;
}