Android Open Source - Munin-for-Android Munin Server






From Project

Back to project page Munin-for-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project Munin-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.chteuchteu.munin.obj;
//from  w  ww .jav a 2 s  . co  m
import android.net.Uri;

import com.chteuchteu.munin.MuninFoo;
import com.chteuchteu.munin.hlpr.Util;
import com.chteuchteu.munin.hlpr.Util.SpecialBool;
import com.chteuchteu.munin.obj.MuninPlugin.AlertState;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class MuninServer {
  private long id;
  private String name;
  private String serverUrl;
  private List<MuninPlugin> plugins;
  private String graphURL;
  private String hdGraphURL;
  private int position;
  public MuninMaster master;
  public boolean isPersistant = false;
  /**
   * Used for Alerts (display if server is unreachable)
   */
  public SpecialBool reachable;
  
  private List<MuninPlugin> erroredPlugins;
  private List<MuninPlugin> warnedPlugins;
  
  public MuninServer() {
    this.name = "";
    this.serverUrl = "";
    this.plugins = new ArrayList<>();
    this.graphURL = "";
    this.hdGraphURL = "";
    this.position = -1;
    this.erroredPlugins = new ArrayList<>();
    this.warnedPlugins = new ArrayList<>();
    this.reachable = SpecialBool.UNKNOWN;
  }
  public MuninServer (String name, String serverUrl) {
    this.name = name;
    this.serverUrl = serverUrl;
    this.plugins = new ArrayList<>();
    this.graphURL = "";
    this.hdGraphURL = "";
    this.position = -1;
    this.erroredPlugins = new ArrayList<>();
    this.warnedPlugins = new ArrayList<>();
    this.reachable = SpecialBool.UNKNOWN;
    generatePosition();
  }
  
  public enum AuthType {
    UNKNOWN(-2), NONE(-1), BASIC(1), DIGEST(2);
    private int val = -2;
    
    AuthType(int val) { this.val = val; }
    public int getVal() { return this.val; }
    public String toString() { return val + ""; }
    public static AuthType get(int val) {
      for (AuthType t : AuthType.values())
        if (t.val == val)
          return t;
      return UNKNOWN;
    }
  }
  
  public void setId(long id) { this.id = id; }
  public long getId() { return this.id; }
  
  public void setServerUrl(String u) { this.serverUrl = u; }
  public String getServerUrl() { return this.serverUrl; }
  
  public void setName(String n) { this.name = n; }
  public String getName() { return this.name; }
  
  public void setGraphURL(String url) { this.graphURL = url; }
  public String getGraphURL() { return this.graphURL; }

  public void setHdGraphURL(String url) { this.hdGraphURL = url; }
  public String getHdGraphURL() { return this.hdGraphURL == null ? "" : this.hdGraphURL; }
  
  public void setPluginsList(List<MuninPlugin> pL) { this.plugins = pL; }
  public List<MuninPlugin> getPlugins() { return this.plugins; }
  
  public void setPosition(int position) { this.position = position; }
  public int getPosition() { return this.position; }

  public List<MuninPlugin> getErroredPlugins() { return this.erroredPlugins; }
  public List<MuninPlugin> getWarnedPlugins() { return this.warnedPlugins; }
  
  public void addPlugin(MuninPlugin plugin) {
    plugin.setInstalledOn(this);
    this.plugins.add(plugin);
  }
  
  public void setParent(MuninMaster p) {
    this.master = p;
    if (p != null && !p.getChildren().contains(this))
      p.addChild(this);
  }
  public MuninMaster getParent() { return this.master; }
  
  
  public List<MuninPlugin> getPluginsList(String userAgent) {
    List<MuninPlugin> mp = new ArrayList<>();
    String html = this.master.grabUrl(this.getServerUrl(), userAgent).html;
    
    if (html.equals(""))
      return null;
    
    //               code  base_uri
    Document doc = Jsoup.parse(html, this.getServerUrl());
    Elements images = doc.select("img[src$=-day.png]");
    
    for (Element image : images) {
      String pluginName = image.attr("src").substring(image.attr("src").lastIndexOf('/') + 1, image.attr("src").lastIndexOf('-'));
      // Delete special chars
      pluginName = pluginName.replace("&", "");
      pluginName = pluginName.replace("^", "");
      pluginName = pluginName.replace("\"", "");
      pluginName = pluginName.replace(",", "");
      pluginName = pluginName.replace(";", "");
      String fancyName = image.attr("alt");
      // Delete quotes
      fancyName = fancyName.replaceAll("\"", "");
      
      // Get graphUrl
      Element link = image.parent();
      String pluginPageUrl = link.attr("abs:href");
      
      // Get groupName
      String group = "";
      if (html.contains("MunStrap")) {
        Element tab = image.parent().parent().parent().parent();
        group = tab.id();
      } else {
        // Munin 2.X
        boolean is2 = true;
        Element table = image.parent().parent().parent().parent().parent();
        
        if (table != null) {
          Element h3 = table.previousElementSibling();
          if (h3 != null)
            group = h3.html();
          else
            is2 = false;
        } else
          is2 = false;
        
        // Munin 1.4
        if (!is2) {
          try {
            Element h3 = image.parent().parent().parent().parent().child(0).child(0).child(0);
            group = h3.html();
          }
          catch (Exception e) { e.printStackTrace(); }
        }
      }
      
      MuninPlugin currentPl = new MuninPlugin(pluginName, this);
      currentPl.setFancyName(fancyName);
      currentPl.setCategory(group);
      currentPl.setPluginPageUrl(pluginPageUrl);
      
      mp.add(currentPl);

      // Find GraphURL
      if (this.graphURL.equals("")) {
        String srcAttr = image.attr("abs:src");
        this.graphURL = srcAttr.substring(0, srcAttr.lastIndexOf('/') + 1);
      }

      // Find HDGraphURL (if not already done)
      if ((this.hdGraphURL == null || this.hdGraphURL.equals("")) && this.master.isDynazoomAvailable() != MuninMaster.DynazoomAvailability.FALSE) {
        // To go to the dynazoom page, we have to "click" on the first graph.
        // Then, on the second page, we have to "click" again on the first graph.
        // Finally, the only image on this third page is the dynazoom graph.
        String srcAttr = image.parent().attr("abs:href");

        String secondPageHtml = this.master.grabUrl(srcAttr, userAgent).html;
        Document secondPage = Jsoup.parse(secondPageHtml, srcAttr);

        Elements images2 = secondPage.select("img[src$=-day.png]");
        if (images2.size() > 0) {
          Element imageParent = images2.get(0).parent();
          if (imageParent.tagName().equals("a")) {
            String srcAttr2 = imageParent.attr("abs:href");
            String thirdPageHtml = this.master.grabUrl(srcAttr2, userAgent).html;
            if (thirdPageHtml.equals(""))
              this.master.setDynazoomAvailable(MuninMaster.DynazoomAvailability.FALSE);
            else {
              // Since the image URL is built in JS on the web page, we have to build it manually
              Uri uri = Uri.parse(srcAttr2);
              String cgiUrl = uri.getQueryParameterNames().contains("cgiurl_graph") ? uri.getQueryParameter("cgiurl_graph")
                  : "/munin-cgi/munin-cgi-graph";
              // localdomain/localhost.localdomain/if_eth0
              String pluginNameUrl = uri.getQueryParameterNames().contains("plugin_name") ? uri.getQueryParameter("plugin_name")
                  : "localdomain/localhost.localdomain/pluginName";
              // Remove plugin name from pluginNameUrl
              pluginNameUrl = pluginNameUrl.substring(0, pluginNameUrl.lastIndexOf('/') + 1);

              this.hdGraphURL = "http://" + Util.URLManipulation.getHostFromUrl(this.getServerUrl()) + cgiUrl + "/" + pluginNameUrl;

              // Now that we have the HD Graph URL, let's try to reach it to see if it is available
              if (this.master.isDynazoomAvailable(currentPl, userAgent))
                this.master.setDynazoomAvailable(MuninMaster.DynazoomAvailability.TRUE);
              else {
                this.hdGraphURL = "";
                this.master.setDynazoomAvailable(MuninMaster.DynazoomAvailability.FALSE);
              }
            }
          } else
            this.master.setDynazoomAvailable(MuninMaster.DynazoomAvailability.FALSE);
        } else
          this.master.setDynazoomAvailable(MuninMaster.DynazoomAvailability.FALSE);
      }
    }
    return mp;
  }
  
  public boolean fetchPluginsList(String userAgent) {
    List<MuninPlugin> plugins = getPluginsList(userAgent);
    
    if (plugins != null) {
      this.plugins = plugins;
      return true;
    }
    return false;
  }

  /**
   * Get plugin state (OK / WARNING / CRITICAL) for each plugin
   * in this server. Used on Activity_Alerts
   * @param userAgent String
   */
  public void fetchPluginsStates(String userAgent) {
    erroredPlugins.clear();
    warnedPlugins.clear();
    
    // Set all to undefined
    for (MuninPlugin plugin : this.plugins)
      plugin.setState(AlertState.UNDEFINED);
    
    HTTPResponse response = master.grabUrl(this.getServerUrl(), userAgent);
    
    if (response.timeout || response.responseCode != 200 || response.html.equals(""))
      this.reachable = SpecialBool.FALSE;
    else {
      this.reachable = SpecialBool.TRUE;
      
      Document doc = Jsoup.parse(response.html, this.getServerUrl());
      Elements images = doc.select("img[src$=-day.png]");
      
      for (Element image : images) {
        String pluginName = image.attr("src").substring(image.attr("src").lastIndexOf('/') + 1, image.attr("src").lastIndexOf('-'));
        
        MuninPlugin plugin = null;
        // Plugin lookup
        for (MuninPlugin m : this.plugins) {
          if (m.getName().equals(pluginName)) {
            plugin = m; break;
          }
        }
        if (plugin != null) {
          if (image.hasClass("crit") || image.hasClass("icrit")) {
            plugin.setState(AlertState.CRITICAL);
            erroredPlugins.add(plugin);
          }
          else if (image.hasClass("warn") || image.hasClass("iwarn")) {
            plugin.setState(AlertState.WARNING);
            warnedPlugins.add(plugin);
          }
          else
            plugin.setState(AlertState.OK);
        }
      }
    }
  }
  
  public MuninPlugin getPlugin(int pos) {
    if (pos < this.plugins.size() && pos >= 0)
      return this.plugins.get(pos);
    else
      return null;
  }
  
  public MuninPlugin getPlugin(String pluginName) {
    for (MuninPlugin plugin : this.plugins) {
      if (plugin.getName().equals(pluginName))
        return plugin;
    }
    return null;
  }
  
  public int getPosition(MuninPlugin p) {
    for (int i=0; i<this.plugins.size(); i++) {
      if (p.equalsApprox(this.plugins.get(i)))
        return i;
    }
    return 0;
  }
  
  
  private void generatePosition() {
    MuninFoo muninFoo = MuninFoo.getInstance();
    // Si toutes positions == -1 -> pos = 0
    int nbNotNull = 0;
    
    for (int i=0; i<muninFoo.getServers().size(); i++) {
      if (muninFoo.getServer(i) != null && muninFoo.getServer(i).getPosition() != -1)
        nbNotNull++;
    }
    if (nbNotNull == 0)
      this.position = 0;

        // Save the latest position
        int higherPosition = -1;
        for (int i=0; i<muninFoo.getServers().size(); i++) {
            if (muninFoo.getServer(i) != null && muninFoo.getServer(i).getPosition() > higherPosition)
                higherPosition = muninFoo.getServer(i).getPosition();
        }
        this.position = higherPosition + 1;
  }
  
  private List<MuninPlugin> getPluginsByCategory(String c) {
    List<MuninPlugin> l = new ArrayList<>();
    for (MuninPlugin p : plugins) {
      if (p.getCategory() != null && p.getCategory().equals(c))
        l.add(p);
    }
    return l;
  }
  
  private List<String> getDistinctCategories() {
    List<String> list = new ArrayList<>();
    
    for (MuninPlugin plugin : plugins) {
      if (plugin.getCategory() == null)
        continue;

      // Check if list already contains
      boolean contains = false;
      for (String s : list) {
        if (plugin.getCategory().equals(s))
          contains = true;
      }
      if (!contains)
        list.add(plugin.getCategory());
    }
    return list;
  }
  
  public List<List<MuninPlugin>> getPluginsListWithCategory() {
    List<List<MuninPlugin>> l = new ArrayList<>();
    for (String s : getDistinctCategories()) {
      l.add(getPluginsByCategory(s));
    }
    return l;
  }
  
  public boolean equalsApprox (MuninServer server2) {
    String address1 = this.getServerUrl();
    String address2 = server2.getServerUrl();
    
    // transformations
    if (address1.length() > 11) {
      if (address1.endsWith("index.html"))
        address1 = address1.substring(0, address1.length()-11);
      if (address1.substring(address1.length()-1).equals("/"))
        address1 = address1.substring(0, address1.length()-1);
    }
    if (address2.length() > 11) {
      if (address2.endsWith("index.html"))
        address2 = address2.substring(0, address2.length()-11);
      if (address2.substring(address2.length()-1).equals("/"))
        address2 = address2.substring(0, address2.length()-1);
    }
    return address1.equals(address2);
  }
  
  public boolean equalsApprox (String server2) {
    String address1 = this.getServerUrl();
    String address2 = server2;
    
    // transformations
    if (address1.length() > 11) {
      if (address1.endsWith("index.html"))
        address1 = address1.substring(0, address1.length()-11);
      if (address1.substring(address1.length()-1).equals("/"))
        address1 = address1.substring(0, address1.length()-1);
    }
    if (address2.length() > 11) {
      if (address2.endsWith("index.html"))
        address2 = address2.substring(0, address2.length()-11);
      if (address2.substring(address2.length()-1).equals("/"))
        address2 = address2.substring(0, address2.length()-1);
    }
    return address1.equals(address2);
  }
}




Java Source Code List

com.chteuchteu.munin.BootReceiver.java
com.chteuchteu.munin.CustomSSLFactory.java
com.chteuchteu.munin.MuninFoo.java
com.chteuchteu.munin.Service_Notifications.java
com.chteuchteu.munin.adptr.Adapter_ExpandableListView.java
com.chteuchteu.munin.adptr.Adapter_GraphView.java
com.chteuchteu.munin.adptr.Adapter_IconList.java
com.chteuchteu.munin.adptr.Adapter_SeparatedList.java
com.chteuchteu.munin.exc.ImportExportWebserviceException.java
com.chteuchteu.munin.exc.NullMuninFooException.java
com.chteuchteu.munin.exc.TrialExpirationDateReached.java
com.chteuchteu.munin.hlpr.BillingService.java
com.chteuchteu.munin.hlpr.DatabaseHelper.java
com.chteuchteu.munin.hlpr.DigestUtils.java
com.chteuchteu.munin.hlpr.DocumentationHelper.java
com.chteuchteu.munin.hlpr.DrawerHelper.java
com.chteuchteu.munin.hlpr.DynazoomHelper.java
com.chteuchteu.munin.hlpr.EncryptionHelper.java
com.chteuchteu.munin.hlpr.GridDownloadHelper.java
com.chteuchteu.munin.hlpr.I18nHelper.java
com.chteuchteu.munin.hlpr.ImportExportHelper.java
com.chteuchteu.munin.hlpr.JSONHelper.java
com.chteuchteu.munin.hlpr.MediaScannerUtil.java
com.chteuchteu.munin.hlpr.NetHelper.java
com.chteuchteu.munin.hlpr.SQLite.java
com.chteuchteu.munin.hlpr.Util.java
com.chteuchteu.munin.obj.AlertsWidget.java
com.chteuchteu.munin.obj.GraphWidget.java
com.chteuchteu.munin.obj.GridItem.java
com.chteuchteu.munin.obj.Grid.java
com.chteuchteu.munin.obj.HTTPResponse_Bitmap.java
com.chteuchteu.munin.obj.HTTPResponse.java
com.chteuchteu.munin.obj.Label.java
com.chteuchteu.munin.obj.MuninMaster.java
com.chteuchteu.munin.obj.MuninPlugin.java
com.chteuchteu.munin.obj.MuninServer.java
com.chteuchteu.munin.obj.SearchResult.java
com.chteuchteu.munin.ui.Activity_About.java
com.chteuchteu.munin.ui.Activity_AlertsPluginSelection.java
com.chteuchteu.munin.ui.Activity_Alerts.java
com.chteuchteu.munin.ui.Activity_GoPremium.java
com.chteuchteu.munin.ui.Activity_GraphView.java
com.chteuchteu.munin.ui.Activity_Grid.java
com.chteuchteu.munin.ui.Activity_Grids.java
com.chteuchteu.munin.ui.Activity_Label.java
com.chteuchteu.munin.ui.Activity_Labels.java
com.chteuchteu.munin.ui.Activity_Main.java
com.chteuchteu.munin.ui.Activity_Notifications.java
com.chteuchteu.munin.ui.Activity_Plugins.java
com.chteuchteu.munin.ui.Activity_Server.java
com.chteuchteu.munin.ui.Activity_ServersEdit.java
com.chteuchteu.munin.ui.Activity_Servers.java
com.chteuchteu.munin.ui.Activity_Settings.java
com.chteuchteu.munin.ui.HackyDrawerLayout.java
com.chteuchteu.munin.ui.MuninActivity.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_Configure.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_ViewsFactory.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_WidgetProvider.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_WidgetService.java
com.chteuchteu.munin.wdget.Widget_GraphWidget_Configure.java
com.chteuchteu.munin.wdget.Widget_GraphWidget_WidgetProvider.java
com.mobeta.android.dslv.DragSortController.java
com.mobeta.android.dslv.DragSortCursorAdapter.java
com.mobeta.android.dslv.DragSortItemViewCheckable.java
com.mobeta.android.dslv.DragSortItemView.java
com.mobeta.android.dslv.DragSortListView.java
com.mobeta.android.dslv.ResourceDragSortCursorAdapter.java
com.mobeta.android.dslv.SimpleDragSortCursorAdapter.java
com.mobeta.android.dslv.SimpleFloatViewManager.java
org.taptwo.android.widget.CircleFlowIndicator.java
org.taptwo.android.widget.FlowIndicator.java
org.taptwo.android.widget.TitleFlowIndicator.java
org.taptwo.android.widget.TitleProvider.java
org.taptwo.android.widget.ViewFlow.java
uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.IPhotoView.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.SDK16.java
uk.co.senab.photoview.ScrollerProxy.java
uk.co.senab.photoview.VersionedGestureDetector.java