Search in sources :

Example 1 with CsvReader

use of com.zimbra.common.util.CsvReader in project zm-mailbox by Zimbra.

the class ServiceStatus method parseData.

public static List<ServiceStatus> parseData(Map<String, CsvReader> data) throws IOException {
    List<ServiceStatus> results = Lists.newArrayList();
    for (Entry<String, CsvReader> entry : data.entrySet()) {
        String host = entry.getKey();
        CsvReader r = entry.getValue();
        List<String> columns = new ArrayList<String>(Arrays.asList(r.getColNames()));
        columns.remove("timestamp");
        Map<String, String> row = Maps.newHashMap();
        String lastTS = null;
        while (r.hasNext()) {
            String ts = r.getValue("timestamp");
            boolean rowHasData = false;
            for (String column : columns) {
                String value = r.getValue(column);
                rowHasData = rowHasData || value != null;
            }
            if (rowHasData) {
                lastTS = ts;
                row.clear();
                for (String column : columns) {
                    String value = r.getValue(column);
                    if (value != null)
                        row.put(column, value);
                }
            }
        }
        if (lastTS != null) {
            long timeStamp = Long.parseLong(lastTS);
            for (Entry<String, String> svcStatusEntry : row.entrySet()) {
                String service = svcStatusEntry.getKey();
                String status = svcStatusEntry.getValue();
                if (status != null) {
                    status = status.trim();
                    // for some reason, QA is getting an empty string
                    if ("".equals(status))
                        continue;
                    ServiceStatus s = fromServerServiceTimeStatus(host, service, timeStamp, // happens when a service has just started
                    ZeroOrOne.fromBool(Float.parseFloat(status) > 0.8));
                    results.add(s);
                }
            }
        }
    }
    return results;
}
Also used : CsvReader(com.zimbra.common.util.CsvReader) ArrayList(java.util.ArrayList)

Example 2 with CsvReader

use of com.zimbra.common.util.CsvReader in project zm-mailbox by Zimbra.

the class ChartUtil method readCsvFiles.

private void readCsvFiles() throws Exception {
    Date minDate = null;
    Date maxDate = null;
    // per-file--shouldn't happen much since this is only for groupplot
    for (DataColumn c : mUniqueStringColumns) {
        String inFilename = c.getInfile();
        Reader reader = null;
        StringSeries data = mStringSeries.get(c);
        try {
            reader = new MultipleDirsFileReader(inFilename, mSrcDirs);
        } catch (FileNotFoundException e) {
            System.out.printf("CSV file %s not found; Skipping...\n", inFilename);
            continue;
        }
        CsvReader csv = null;
        try {
            csv = new CsvReader(reader);
            int line = 1;
            while (csv.hasNext()) {
                line++;
                String ctx = inFilename + ", line " + line;
                Date ts = null;
                try {
                    ts = readTS(csv, ctx);
                } catch (ParseException e) {
                    if (e.getMessage().compareTo("Unparseable date: \"timestamp\"") != 0) {
                        // bug 54626, ignored the repeat timestamp header
                        System.out.println(ctx + ": " + e);
                    }
                    continue;
                }
                if (ts.before(mStartAt) || ts.after(mEndAt))
                    continue;
                if (minDate == null) {
                    minDate = ts;
                    maxDate = ts;
                } else {
                    if (ts.compareTo(minDate) < 0)
                        minDate = ts;
                    if (ts.compareTo(maxDate) > 0)
                        maxDate = ts;
                }
                String value = csv.getValue(c.getColumn());
                data.AddEntry(ts, value);
            }
        } finally {
            if (csv != null)
                csv.close();
        }
    }
    // Read CSVs and populate data series.
    for (Iterator<Map.Entry<String, Set<Pair<String, DataSeries>>>> mapIter = mColumnsByInfile.entrySet().iterator(); mapIter.hasNext(); ) {
        Map.Entry<String, Set<Pair<String, DataSeries>>> entry = mapIter.next();
        String inFilename = entry.getKey();
        Set<Pair<String, DataSeries>> columns = entry.getValue();
        System.out.println("Reading CSV " + inFilename);
        Reader reader = null;
        try {
            reader = new MultipleDirsFileReader(inFilename, mSrcDirs);
        } catch (FileNotFoundException e) {
            System.out.printf("CSV file %s not found; Skipping...\n", inFilename);
            continue;
        }
        CsvReader csv = null;
        try {
            csv = new CsvReader(reader);
            int line = 1;
            while (csv.hasNext()) {
                line++;
                String context = inFilename + ", line " + line;
                Date ts = null;
                try {
                    ts = readTS(csv, context);
                } catch (ParseException e) {
                    if (e.getMessage().compareTo("Unparseable date: \"timestamp\"") != 0) {
                        // bug 54626, ignored the repeat timestamp header
                        System.out.println(context + ": " + e);
                    }
                    continue;
                }
                if (ts.before(mStartAt) || ts.after(mEndAt))
                    continue;
                // Set min/max date
                if (minDate == null) {
                    minDate = ts;
                    maxDate = ts;
                } else {
                    if (ts.compareTo(minDate) < 0) {
                        minDate = ts;
                    }
                    if (ts.compareTo(maxDate) > 0) {
                        maxDate = ts;
                    }
                }
                // Parse values
                for (Iterator<Pair<String, DataSeries>> colIter = columns.iterator(); colIter.hasNext(); ) {
                    Pair<String, DataSeries> colSeries = colIter.next();
                    String column = colSeries.getFirst();
                    DataSeries series = colSeries.getSecond();
                    String val = csv.getValue(column);
                    if (!StringUtil.isNullOrEmpty(val)) {
                        try {
                            double d = Double.parseDouble(val);
                            try {
                                series.AddEntry(ts, d);
                            } catch (SeriesException e) {
                                System.out.printf("Can't add sample to series: timestamp=%s, value=%s\n", ts, val);
                                e.printStackTrace(System.out);
                            }
                        } catch (NumberFormatException e) {
                            System.out.println(String.format("%s: unable to parse value '%s' for %s: %s", context, val, column, e));
                        }
                    } else {
                        // default an entry to 0 if string is empty
                        series.AddEntry(ts, 0.0);
                    }
                }
            }
            for (Iterator<Pair<String, DataSeries>> colIter = columns.iterator(); colIter.hasNext(); ) {
                Pair<String, DataSeries> colSeries = colIter.next();
                String column = colSeries.getFirst();
                DataSeries series = colSeries.getSecond();
                System.out.format("Adding %d %s points between %s and %s.\n\n", series.size(), column, minDate, maxDate);
            }
        } finally {
            if (csv != null)
                csv.close();
        }
    }
    adustSampleRange(minDate, maxDate);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) CsvReader(com.zimbra.common.util.CsvReader) FileReader(java.io.FileReader) SeriesException(org.jfree.data.general.SeriesException) Date(java.util.Date) CsvReader(com.zimbra.common.util.CsvReader) ParseException(java.text.ParseException) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.zimbra.common.util.Pair)

Example 3 with CsvReader

use of com.zimbra.common.util.CsvReader in project zm-mailbox by Zimbra.

the class SummaryAnalyzer method writeReport.

public void writeReport(File summaryCsv) throws IOException {
    buildIOKeys();
    buildCPUKeys();
    buildMemKeys();
    buildServerResponseKeys();
    FileReader reader = null;
    CsvReader csv = null;
    FileWriter writer = null;
    try {
        reader = new FileReader(summaryCsv);
        csv = new CsvReader(reader);
        File summaryTxt = new File(summaryCsv.getParentFile(), SummaryConstants.SUMMARY_TXT);
        writer = new FileWriter(summaryTxt);
        writer.write("===========================================\n");
        writer.write("#            Perf Stats                   #\n");
        writer.write("===========================================\n");
        if (csv.hasNext()) {
            for (ReportEntry entry : mEntryList) {
                if (csv.columnExists(entry.entryKey)) {
                    writer.write(entry.entryName + ":" + csv.getValue(entry.entryKey));
                } else {
                    writer.write(entry.entryName + ":");
                }
                writer.write("\n");
            }
        }
    } finally {
        if (writer != null)
            writer.close();
        if (csv != null)
            csv.close();
        else if (reader != null)
            reader.close();
    }
}
Also used : CsvReader(com.zimbra.common.util.CsvReader) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) File(java.io.File)

Example 4 with CsvReader

use of com.zimbra.common.util.CsvReader in project zm-mailbox by Zimbra.

the class GetServiceStatus method handle.

public Element handle(Element request, Map<String, Object> context) throws SoapFaultException, ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    // this command can only execute on the monitor host, so proxy if necessary
    Provisioning prov = Provisioning.getInstance();
    String monitorHost = prov.getConfig().getAttr(Provisioning.A_zimbraLogHostname);
    if (monitorHost == null || monitorHost.trim().equals(""))
        throw ServiceException.FAILURE("zimbraLogHostname is not configured", null);
    Server monitorServer = prov.get(Key.ServerBy.name, monitorHost);
    if (monitorServer == null)
        throw ServiceException.FAILURE("could not find zimbraLogHostname server: " + monitorServer, null);
    if (!prov.getLocalServer().getId().equalsIgnoreCase(monitorServer.getId()))
        return proxyRequest(request, context, monitorServer);
    GetServiceStatusResponse resp = new GetServiceStatusResponse();
    TimeZone tz = TimeZone.getDefault();
    TimeZoneInfo timezone = TimeZoneInfo.fromIdAndDisplayName(tz.getID(), tz.getDisplayName());
    resp.setTimezone(timezone);
    boolean loggerEnabled = false;
    Server local = prov.getLocalServer();
    String[] services = local.getMultiAttr(Provisioning.A_zimbraServiceEnabled);
    if (services != null) {
        for (int i = 0; i < services.length && !loggerEnabled; i++) {
            loggerEnabled = "logger".equals(services[i]);
        }
    }
    if (loggerEnabled) {
        HashSet<ServiceStatus> serviceStatus = Sets.newHashSet();
        List<Server> servers = prov.getAllServers();
        for (Server s : servers) {
            String[] srvs = s.getMultiAttr(Provisioning.A_zimbraServiceEnabled);
            for (String service : srvs) {
                serviceStatus.add(ServiceStatus.fromServerServiceTimeStatus(s.getName(), service, System.currentTimeMillis() / 1000, ZeroOrOne.ZERO));
            }
        }
        BufferedReader in = null;
        try {
            ProcessBuilder pb = new ProcessBuilder(ZMRRDFETCH, "-f", ZMSTATUSLOG_CSV);
            Process p = pb.start();
            in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            Map<String, CsvReader> hostStatus = new HashMap<String, CsvReader>();
            StringWriter currentWriter = null;
            String currentHost = null;
            String line;
            while ((line = in.readLine()) != null) {
                if ("".equals(line.trim()))
                    continue;
                if (line.startsWith("Host: ")) {
                    if (currentHost != null)
                        hostStatus.put(currentHost, new CsvReader(new StringReader(currentWriter.toString())));
                    currentHost = line.substring("Host: ".length());
                    currentWriter = new StringWriter();
                } else {
                    if (currentWriter != null)
                        currentWriter.write(line + "\n");
                }
            }
            if (currentHost != null && currentWriter != null)
                hostStatus.put(currentHost, new CsvReader(new StringReader(currentWriter.toString())));
            List<ServiceStatus> status = ServiceStatus.parseData(hostStatus);
            for (ServiceStatus stat : status) {
                serviceStatus.remove(stat);
                serviceStatus.add(stat);
            }
            for (ServiceStatus stat : serviceStatus) {
                if (!checkRights(zsc, stat.getServer())) {
                    ZimbraLog.misc.info("skipping server " + stat.getServer() + ", has not right to get service status");
                    continue;
                }
                resp.addServiceStatus(stat);
            }
        } catch (IOException e) {
            try {
                if (in != null)
                    in.close();
            } catch (IOException x) {
            }
            ServiceException.FAILURE("Unable to read logger stats", e);
        }
    }
    return zsc.jaxbToElement(resp);
}
Also used : Server(com.zimbra.cs.account.Server) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) TimeZoneInfo(com.zimbra.soap.admin.type.TimeZoneInfo) IOException(java.io.IOException) Provisioning(com.zimbra.cs.account.Provisioning) CsvReader(com.zimbra.common.util.CsvReader) TimeZone(java.util.TimeZone) StringWriter(java.io.StringWriter) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ServiceStatus(com.zimbra.soap.admin.type.ServiceStatus) GetServiceStatusResponse(com.zimbra.soap.admin.message.GetServiceStatusResponse) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader)

Aggregations

CsvReader (com.zimbra.common.util.CsvReader)4 FileReader (java.io.FileReader)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 Pair (com.zimbra.common.util.Pair)1 Provisioning (com.zimbra.cs.account.Provisioning)1 Server (com.zimbra.cs.account.Server)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 GetServiceStatusResponse (com.zimbra.soap.admin.message.GetServiceStatusResponse)1 ServiceStatus (com.zimbra.soap.admin.type.ServiceStatus)1 TimeZoneInfo (com.zimbra.soap.admin.type.TimeZoneInfo)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 ParseException (java.text.ParseException)1