GetServerStats.java 6.24 KB
Newer Older
1 2 3 4
/**
 * $Revision: $
 * $Date: $
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
18 19
 */

20
package org.jivesoftware.openfire.commands.admin;
Gaston Dombiak's avatar
Gaston Dombiak committed
21 22 23

import org.dom4j.Element;
import org.jivesoftware.admin.AdminConsole;
24 25 26 27 28 29
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.openfire.component.InternalComponentManager;
import org.jivesoftware.openfire.session.ClientSession;
30
import org.jivesoftware.util.LocaleUtils;
31
import org.jivesoftware.util.XMPPDateTimeFormat;
Gaston Dombiak's avatar
Gaston Dombiak committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;

import java.text.DecimalFormat;
import java.util.*;

/**
 * Command that returns information about the server and some basic statistics. This command
 * can only be executed by administrators or components of the server.
 *
 * TODO Use i18n
 * 
 * @author Gaston Dombiak
 */
public class GetServerStats extends AdHocCommand {

49 50
    @Override
	protected void addStageInformation(SessionData data, Element command) {
Gaston Dombiak's avatar
Gaston Dombiak committed
51 52 53
        //Do nothing since there are no stages
    }

54 55
    @Override
	public void execute(SessionData data, Element command) {
Gaston Dombiak's avatar
Gaston Dombiak committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        DataForm form = new DataForm(DataForm.Type.result);

        FormField field = form.addField();
        field.setType(FormField.Type.hidden);
        field.setVariable("FORM_TYPE");
        field.addValue("http://jabber.org/protocol/admin");

        field = form.addField();
        field.setLabel(LocaleUtils.getLocalizedString("index.server_name"));
        field.setVariable("name");
        field.addValue(AdminConsole.getAppName());

        field = form.addField();
        field.setLabel(LocaleUtils.getLocalizedString("index.version"));
        field.setVariable("version");
        field.addValue(AdminConsole.getVersionString());

        field = form.addField();
        field.setLabel(LocaleUtils.getLocalizedString("index.domain_name"));
        field.setVariable("domain");
76
        field.addValue(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
Gaston Dombiak's avatar
Gaston Dombiak committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

        field = form.addField();
        field.setLabel(LocaleUtils.getLocalizedString("index.jvm"));
        field.setVariable("os");
        String vmName = System.getProperty("java.vm.name");
        if (vmName == null) {
            vmName = "";
        }
        else {
            vmName = " -- " + vmName;
        }
        field.addValue(System.getProperty("java.version") + " " +System.getProperty("java.vendor") +vmName);

        field = form.addField();
        field.setLabel(LocaleUtils.getLocalizedString("index.uptime"));
        field.setVariable("uptime");
93
        field.addValue(XMPPDateTimeFormat.format(XMPPServer.getInstance().getServerInfo().getLastStarted()));
Gaston Dombiak's avatar
Gaston Dombiak committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

        DecimalFormat mbFormat = new DecimalFormat("#0.00");
        DecimalFormat percentFormat = new DecimalFormat("#0.0");
        Runtime runtime = Runtime.getRuntime();
        double freeMemory = (double)runtime.freeMemory()/(1024*1024);
        double maxMemory = (double)runtime.maxMemory()/(1024*1024);
        double totalMemory = (double)runtime.totalMemory()/(1024*1024);
        double usedMemory = totalMemory - freeMemory;
        double percentFree = ((maxMemory - usedMemory)/maxMemory)*100.0;
        double percentUsed = 100 - percentFree;
        field = form.addField();
        field.setLabel(LocaleUtils.getLocalizedString("index.memory"));
        field.setVariable("memory");
        field.addValue(mbFormat.format(usedMemory) + " MB of " + mbFormat.format(maxMemory) + " MB (" +
                percentFormat.format(percentUsed) + "%) used");

        // Make sure that we are only counting based on bareJIDs and not fullJIDs
        Collection<ClientSession> sessions = SessionManager.getInstance().getSessions();
        Set<String> users = new HashSet<String>(sessions.size());
        int availableSessions = 0;
        for (ClientSession session : sessions) {
            if (session.getPresence().isAvailable()) {
                users.add(session.getAddress().toBareJID());
                availableSessions++;
            }
        }
        field = form.addField();
        field.setLabel("Available Users");
        field.setVariable("activeusersnum");
        field.addValue(users.size());

        field = form.addField();
        field.setLabel("Available Users Sessions");
        field.setVariable("sessionsnum");
        field.addValue(availableSessions);

        command.add(form.getElement());
    }

133 134
    @Override
	protected List<Action> getActions(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
135 136 137 138
        //Do nothing since there are no stages
        return null;
    }

139 140
    @Override
	public String getCode() {
Gaston Dombiak's avatar
Gaston Dombiak committed
141 142 143
        return "http://jabber.org/protocol/admin#get-server-stats";
    }

144 145
    @Override
	public String getDefaultLabel() {
Gaston Dombiak's avatar
Gaston Dombiak committed
146 147 148
        return "Get basic statistics of the server.";
    }

149 150
    @Override
	protected Action getExecuteAction(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
151 152 153 154
        //Do nothing since there are no stages
        return null;
    }

155 156
    @Override
	public int getMaxStages(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
157 158 159 160 161 162 163 164 165 166
        return 0;
    }

    /**
     * Returns if the requester can access this command. Only admins and components
     * are allowed to execute this command.
     *
     * @param requester the JID of the user requesting to execute this command.
     * @return true if the requester can access this command.
     */
167 168
    @Override
	public boolean hasPermission(JID requester) {
169
        return super.hasPermission(requester) || InternalComponentManager.getInstance().hasComponent(requester);
Gaston Dombiak's avatar
Gaston Dombiak committed
170 171
    }
}