Search in sources :

Example 21 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project openj9 by eclipse.

the class TestManagementFactory method testExtOperatingSystemMXBeanProxyIsNotificationEmitter.

/**
 * For IBM extension on the OperatingSystemMXBean type
 */
@Test
public void testExtOperatingSystemMXBeanProxyIsNotificationEmitter() {
    try {
        com.ibm.lang.management.OperatingSystemMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=OperatingSystem", getOperatingSystemInterface());
        OperatingSystemMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        AssertJUnit.assertFalse(proxy instanceof com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl);
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) IOException(java.io.IOException) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Test(org.testng.annotations.Test)

Example 22 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project openj9 by eclipse.

the class TestManagementFactory method testExtOperatingSystemMXBeanProxyDoesRemoveListeners.

/**
 * For IBM extensions on the OperatingSystemMXBean
 */
@Test
public void testExtOperatingSystemMXBeanProxyDoesRemoveListeners() {
    try {
        com.ibm.lang.management.OperatingSystemMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=OperatingSystem", getOperatingSystemInterface());
        OperatingSystemMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        AssertJUnit.assertFalse(proxy instanceof com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl);
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
        NotificationEmitter proxyEmitter = (NotificationEmitter) proxy;
        MyTestListener listener = new MyTestListener();
        proxyEmitter.addNotificationListener(listener, null, null);
        // Fire off a notification and ensure that the listener receives it.
        try {
            ProcessingCapacityNotificationInfo info = new ProcessingCapacityNotificationInfo(240);
            CompositeData cd = TestUtil.toCompositeData(info);
            Notification notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 12);
            notification.setUserData(cd);
            ((ExtendedOperatingSystemMXBeanImpl) (ManagementFactory.getOperatingSystemMXBean())).sendNotification(notification);
            AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
            // Verify the user data of the notification.
            Notification n = listener.getNotification();
            AssertJUnit.assertNotNull(n);
            verifyProcessingCapacityNotificationUserData(n.getUserData());
            // Remove the listener
            proxyEmitter.removeNotificationListener(listener);
            // Fire off a notification and ensure that the listener does
            // *not* receive it.
            listener.resetNotificationsReceivedCount();
            notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 13);
            notification.setUserData(cd);
            ((ExtendedOperatingSystemMXBeanImpl) (ManagementFactory.getOperatingSystemMXBean())).sendNotification(notification);
            AssertJUnit.assertEquals(0, listener.getNotificationsReceivedCount());
            // ListenerNotFoundException being thrown.
            try {
                proxyEmitter.removeNotificationListener(listener);
                Assert.fail("Should have thrown a ListenerNotFoundException!");
            } catch (ListenerNotFoundException e) {
                logger.debug("ListenerNotFoundException occurred, as expected: " + e.getMessage());
            }
        } catch (MalformedObjectNameException e) {
            Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
            e.printStackTrace();
        }
    } catch (ListenerNotFoundException e) {
        Assert.fail("Unexpected ListenerNotFoundException : " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) CompositeData(javax.management.openmbean.CompositeData) IOException(java.io.IOException) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) NotificationEmitter(javax.management.NotificationEmitter) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) ProcessingCapacityNotificationInfo(com.ibm.lang.management.ProcessingCapacityNotificationInfo) ListenerNotFoundException(javax.management.ListenerNotFoundException) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Test(org.testng.annotations.Test)

Example 23 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project openj9 by eclipse.

the class TestManagementFactory method testExtOperatingSystemMXBeanProxyEmitsNotifications.

/**
 * For IBM extension on the OperatingSystemMXBean
 */
@Test
public void testExtOperatingSystemMXBeanProxyEmitsNotifications() {
    try {
        com.ibm.lang.management.OperatingSystemMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=OperatingSystem", getOperatingSystemInterface());
        OperatingSystemMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        AssertJUnit.assertFalse(proxy instanceof com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl);
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
        NotificationEmitter proxyEmitter = (NotificationEmitter) proxy;
        // Add a listener with a handback object.
        MyTestListener listener = new MyTestListener();
        ArrayList<String> arr = new ArrayList<>();
        arr.add("Save your money for the children.");
        proxyEmitter.addNotificationListener(listener, null, arr);
        // Fire off a notification and ensure that the listener receives it.
        try {
            ProcessingCapacityNotificationInfo info = new ProcessingCapacityNotificationInfo(24);
            CompositeData cd = TestUtil.toCompositeData(info);
            Notification notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 12);
            notification.setUserData(cd);
            ((ExtendedOperatingSystemMXBeanImpl) (ManagementFactory.getOperatingSystemMXBean())).sendNotification(notification);
            AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
            // Verify that the handback is as expected.
            AssertJUnit.assertNotNull(listener.getHandback());
            AssertJUnit.assertSame(arr, listener.getHandback());
            ArrayList<?> arr2 = (ArrayList<?>) listener.getHandback();
            AssertJUnit.assertEquals(1, arr2.size());
            AssertJUnit.assertEquals("Save your money for the children.", arr2.get(0));
        } catch (MalformedObjectNameException e) {
            Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
            e.printStackTrace();
        }
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) NotificationEmitter(javax.management.NotificationEmitter) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) ProcessingCapacityNotificationInfo(com.ibm.lang.management.ProcessingCapacityNotificationInfo) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Test(org.testng.annotations.Test)

Example 24 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project ma-modules-public by infiniteautomation.

the class ServerRestController method getSystemInfo.

@ApiOperation(value = "System Info", notes = "Provides disk use, db sizes and point, event counts", response = Map.class)
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/system-info")
public ResponseEntity<SystemInfoModel> getSystemInfo(HttpServletRequest request) {
    RestProcessResult<SystemInfoModel> result = new RestProcessResult<SystemInfoModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (user.isAdmin()) {
            SystemInfoModel model = new SystemInfoModel();
            // Database size
            model.setSqlDbSizeBytes(Common.databaseProxy.getDatabaseSizeInBytes());
            // Do we have any NoSQL Data
            if (Common.databaseProxy.getNoSQLProxy() != null) {
                String pointValueStoreName = Common.envProps.getString("db.nosql.pointValueStoreName", "mangoTSDB");
                model.setNoSqlDbSizeBytes(Common.databaseProxy.getNoSQLProxy().getDatabaseSizeInBytes(pointValueStoreName));
            }
            // Filedata data
            DirectoryInfo fileDatainfo = DirectoryUtils.getSize(new File(Common.getFiledataPath()));
            model.setFileDataSizeBytes(fileDatainfo.getSize());
            // Point history counts.
            model.setTopPoints(DataPointDao.instance.getTopPointHistoryCounts());
            model.setEventCount(EventDao.instance.getEventCount());
            // Disk Info
            FileSystem fs = FileSystems.getDefault();
            List<DiskInfoModel> disks = new ArrayList<DiskInfoModel>();
            model.setDisks(disks);
            for (Path root : fs.getRootDirectories()) {
                try {
                    FileStore store = Files.getFileStore(root);
                    DiskInfoModel disk = new DiskInfoModel();
                    disk.setName(root.getRoot().toString());
                    disk.setTotalSpaceBytes(store.getTotalSpace());
                    disk.setUsableSpaceBytes(store.getUsableSpace());
                    disks.add(disk);
                } catch (IOException e) {
                }
            }
            // CPU Info
            OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
            model.setLoadAverage(osBean.getSystemLoadAverage());
            // OS Info
            model.setArchitecture(osBean.getArch());
            model.setOperatingSystem(osBean.getName());
            model.setOsVersion(osBean.getVersion());
            return result.createResponseEntity(model);
        } else {
            result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("common.default", "User not admin"));
        }
    }
    return result.createResponseEntity();
}
Also used : Path(java.nio.file.Path) SystemInfoModel(com.serotonin.m2m2.web.mvc.rest.v1.model.system.SystemInfoModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) DiskInfoModel(com.serotonin.m2m2.web.mvc.rest.v1.model.system.DiskInfoModel) IOException(java.io.IOException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) FileStore(java.nio.file.FileStore) FileSystem(java.nio.file.FileSystem) DirectoryInfo(com.serotonin.util.DirectoryInfo) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) File(java.io.File) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project commons by craftercms.

the class VersionMonitor method initOS.

private void initOS() {
    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    operating_system = os.getName() + "-" + os.getVersion();
    os_architecture = os.getArch();
    system_encoding = System.getProperty(FILE_ENCODING_SYSTEM_PROP_KEY);
}
Also used : OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Aggregations

OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)88 RuntimeMXBean (java.lang.management.RuntimeMXBean)26 Method (java.lang.reflect.Method)20 IOException (java.io.IOException)15 MemoryMXBean (java.lang.management.MemoryMXBean)11 MemoryUsage (java.lang.management.MemoryUsage)8 ThreadMXBean (java.lang.management.ThreadMXBean)8 HashMap (java.util.HashMap)8 UnixOperatingSystemMXBean (com.sun.management.UnixOperatingSystemMXBean)7 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)6 Status (com.alibaba.dubbo.common.status.Status)5 File (java.io.File)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Test (org.junit.Test)5 Test (org.testng.annotations.Test)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 UnknownHostException (java.net.UnknownHostException)4 Date (java.util.Date)4 ExtendedOperatingSystemMXBeanImpl (com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl)3