Java tutorial
/* * Copyright (c) 2012, Codice Foundation. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA */ package org.codice.ddf.status; import java.io.IOException; import java.io.PrintWriter; import java.util.Calendar; import java.util.LinkedHashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hyperic.sigar.FileSystem; import org.hyperic.sigar.FileSystemUsage; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarPermissionDeniedException; import org.json.simple.JSONValue; public class DiskService extends HttpServlet { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger("org.codice.ddf.CpuService"); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter writer = response.getWriter(); writer.append(toJSON()); } static Map<String, Object> getDiskStatus() { Map<String, Object> obj = new LinkedHashMap<String, Object>(); Sigar sigar = null; try { sigar = new Sigar(); // call it to make sure the library was loaded sigar.getPid(); Map<String, Object> data = new LinkedHashMap<String, Object>(); FileSystem[] filesystem = null; filesystem = sigar.getFileSystemList(); logger.info("SIGAR FOUND # FSes: " + filesystem.length); for (int i = 0; i < filesystem.length; i++) { Map<String, Object> map = filesystem[i].toMap(); FileSystemUsage fs = sigar.getFileSystemUsage(filesystem[i].getDirName()); map.put("usage", fs.toMap()); data.put("disk" + i, map); } obj.put("data", data); obj.put("time", new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()).toString()); sigar.close(); } catch (SigarPermissionDeniedException e) { logger.log(Level.WARNING, "Permission denied for access to disk(s)."); obj.put("data", "Permission denied for access to disk(s)."); obj.put("time", new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()).toString()); if (sigar != null) { try { sigar.close(); } catch (Throwable t1) { // ignore } finally { sigar = null; } } } catch (Throwable t) { logger.log(Level.SEVERE, "Failed to load sigar", t); if (sigar != null) { try { sigar.close(); } catch (Throwable t1) { // ignore } finally { sigar = null; } } } return obj; } static String toJSON() { return JSONValue.toJSONString(getDiskStatus()); } }