├── apache_log ├── .gitignore ├── __init__.py ├── plugins │ ├── __init__.py │ ├── urls_counter.py │ ├── app_thread.py │ ├── apache_url_stat.py │ ├── options.py │ ├── statsd_sender.py │ └── daemon.py ├── ._.DS_Store └── readme.md ├── af.oracle.metrics ├── .gitignore ├── src │ ├── main │ │ ├── resources │ │ │ ├── templates │ │ │ │ └── index.html │ │ │ ├── log4j.properties │ │ │ └── sqls.xml │ │ └── java │ │ │ └── com │ │ │ └── os │ │ │ └── appfirst │ │ │ ├── Metric.java │ │ │ ├── Log2File.java │ │ │ ├── NagiosOutput.java │ │ │ ├── OracleSqlQuerys.java │ │ │ └── MysqlTestsQueries.java │ └── test │ │ └── java │ │ └── com │ │ └── os │ │ └── appfirst │ │ ├── OracleMetricsTest.java │ │ └── AppTest.java ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── libs │ └── java_statsd_client-0.0.3-jar-with-dependencies.jar ├── mvn_inst_local_repo.sh ├── .springBeans ├── get_oracle_metric.sh ├── build.gradle ├── info.md ├── gradlew.bat └── pom.xml ├── jmx-collector ├── appfirst-jmx-0.5.jar ├── appfirst-jmx-0.5-jar-with-dependencies.jar ├── src │ ├── main │ │ ├── java │ │ │ ├── com │ │ │ │ └── objectstyle │ │ │ │ │ └── appfirst │ │ │ │ │ └── jmx │ │ │ │ │ └── collector │ │ │ │ │ ├── command │ │ │ │ │ ├── ThresholdDefinition.java │ │ │ │ │ ├── VirtualMachineDefinition.java │ │ │ │ │ ├── NoThresholdDefinition.java │ │ │ │ │ ├── ValueDefinition.java │ │ │ │ │ ├── URLVirtualMachineDefinition.java │ │ │ │ │ ├── PatternVirtualMachineDefinition.java │ │ │ │ │ ├── CompositeValueDefinition.java │ │ │ │ │ ├── RangeThresholdDefinition.java │ │ │ │ │ └── Command.java │ │ │ │ │ ├── result │ │ │ │ │ ├── ResultData.java │ │ │ │ │ ├── MBeanDataConverter.java │ │ │ │ │ ├── converter │ │ │ │ │ │ ├── SimpleTypeToStringConverter.java │ │ │ │ │ │ ├── ToStringConverter.java │ │ │ │ │ │ ├── SimpleTypeConverter.java │ │ │ │ │ │ ├── PrimitiveToStringTransformer.java │ │ │ │ │ │ ├── MapConverter.java │ │ │ │ │ │ ├── SetConverter.java │ │ │ │ │ │ ├── CompositeTypeConverter.java │ │ │ │ │ │ └── ArrayTypeConverter.java │ │ │ │ │ ├── NoResultData.java │ │ │ │ │ ├── OpenMBeanDataConverter.java │ │ │ │ │ ├── UnsupportedDataTypeException.java │ │ │ │ │ ├── ValidationErrorResult.java │ │ │ │ │ ├── ParseErrorResult.java │ │ │ │ │ ├── UnsupportedOpenTypeException.java │ │ │ │ │ ├── ErrorResultData.java │ │ │ │ │ ├── ErrorResult.java │ │ │ │ │ ├── ResultProcessor.java │ │ │ │ │ ├── SimpleResultData.java │ │ │ │ │ ├── ResultStatus.java │ │ │ │ │ ├── ArrayResultData.java │ │ │ │ │ ├── Result.java │ │ │ │ │ ├── OpenMBeanDataConverterFactory.java │ │ │ │ │ ├── Primitives.java │ │ │ │ │ ├── CompositeResultData.java │ │ │ │ │ └── MBeanDataConverterFactory.java │ │ │ │ │ ├── output │ │ │ │ │ ├── Output.java │ │ │ │ │ ├── SysOutOutput.java │ │ │ │ │ ├── AfCollectorOutput.java │ │ │ │ │ ├── QueuedOutput.java │ │ │ │ │ └── af │ │ │ │ │ │ └── AfCollector.java │ │ │ │ │ ├── config │ │ │ │ │ ├── CommandLineParserException.java │ │ │ │ │ ├── ThresholdValidationException.java │ │ │ │ │ ├── parser │ │ │ │ │ │ └── CommandLineParser.java │ │ │ │ │ ├── CommandLinesSource.java │ │ │ │ │ ├── CommandValidationException.java │ │ │ │ │ ├── FileReaderInputSupplier.java │ │ │ │ │ ├── CommandLineProcessor.java │ │ │ │ │ ├── Constants.java │ │ │ │ │ ├── FileCommandLinesSource.java │ │ │ │ │ ├── SimpleCommandLinesSource.java │ │ │ │ │ ├── ThresholdBuilder.java │ │ │ │ │ └── CommandBuilder.java │ │ │ │ │ ├── resolve │ │ │ │ │ ├── VirtualMachineIdentifierInvalidationListener.java │ │ │ │ │ ├── VirtualMachineResolverRuntimeException.java │ │ │ │ │ ├── VirtualMachineResolver.java │ │ │ │ │ ├── VirtualMachineMatcher.java │ │ │ │ │ ├── VirtualMachineResolverException.java │ │ │ │ │ ├── VirtualMachineNotFoundException.java │ │ │ │ │ ├── NotUniqueVirtualMachineException.java │ │ │ │ │ ├── URLVirtualMachineResolver.java │ │ │ │ │ ├── StringInclusionVirtualMachineMatcher.java │ │ │ │ │ ├── JvmstatVirtualMachineMatcher.java │ │ │ │ │ ├── LocalVirtualMachineIdentifier.java │ │ │ │ │ ├── VirtualMachineIdentifier.java │ │ │ │ │ └── CachingLocalVirtualMachineResolver.java │ │ │ │ │ ├── execution │ │ │ │ │ ├── CommandExecutor.java │ │ │ │ │ ├── ThresholdChecker.java │ │ │ │ │ ├── CommandExecutionException.java │ │ │ │ │ ├── UnsupportedObjectTypeException.java │ │ │ │ │ ├── DelegatingThresholdChecker.java │ │ │ │ │ ├── RangeThresholdChecker.java │ │ │ │ │ └── CommandsExecutionLifecycle.java │ │ │ │ │ ├── utils │ │ │ │ │ └── UserGroupGetter.java │ │ │ │ │ └── management │ │ │ │ │ ├── JMXConnectorPool.java │ │ │ │ │ └── ManagementConnectionFactory.java │ │ │ ├── TestDeamond.java │ │ │ └── Connect.java │ │ └── resources │ │ │ └── log4j.properties │ └── test │ │ └── java │ │ └── com │ │ └── objectstyle │ │ └── appfirst │ │ └── jmx │ │ └── collector │ │ ├── result │ │ ├── SimpleResultDataTest.java │ │ ├── CompositeResultDataTest.java │ │ └── ArrayResultDataTest.java │ │ ├── utils │ │ └── JVMAttachTest.java │ │ └── config │ │ ├── ThresholdBuilderTest.java │ │ ├── SimpleCommandLinesSourceTest.java │ │ ├── parser │ │ └── RegexBasedCommandLineParserTest.java │ │ └── CommandBuilderTest.java ├── jmx-collector.sh ├── log4j.properties ├── config-examples.cfg └── jmxcollector ├── .gitignore ├── poll2log ├── test ├── test_check_resque.py ├── test_check_passenger.py ├── test_check_redis.py ├── test_check_memcached.py ├── test_check_mysql.py ├── test_check_smart.py ├── test_check_mongodb.py ├── test_check_postgresql.py ├── test_plugin.py └── test_nagios.py ├── Readme_disk_throughput ├── ucommands.py ├── check_passenger.py ├── statsd.py ├── check_ipmi_sensors.ps1 ├── check_zookeeper.py ├── check_cassandra.py ├── check_couchdb.py ├── check_couchbase.py ├── check_haproxy.py └── check_resque.py /apache_log/.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | -------------------------------------------------------------------------------- /apache_log/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | __init__.py 3 | 4 | """ 5 | __version__ = '0.1.0' -------------------------------------------------------------------------------- /apache_log/plugins/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | __init__.py 3 | 4 | """ 5 | __version__ = '0.1.0' -------------------------------------------------------------------------------- /apache_log/._.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appfirst/nagios-plugins/HEAD/apache_log/._.DS_Store -------------------------------------------------------------------------------- /af.oracle.metrics/.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .settings/ 4 | target/ 5 | *.log 6 | *.DS_Store 7 | -------------------------------------------------------------------------------- /jmx-collector/appfirst-jmx-0.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appfirst/nagios-plugins/HEAD/jmx-collector/appfirst-jmx-0.5.jar -------------------------------------------------------------------------------- /af.oracle.metrics/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Hello, world! 4 | 5 | 6 | -------------------------------------------------------------------------------- /af.oracle.metrics/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appfirst/nagios-plugins/HEAD/af.oracle.metrics/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /jmx-collector/appfirst-jmx-0.5-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appfirst/nagios-plugins/HEAD/jmx-collector/appfirst-jmx-0.5-jar-with-dependencies.jar -------------------------------------------------------------------------------- /af.oracle.metrics/libs/java_statsd_client-0.0.3-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appfirst/nagios-plugins/HEAD/af.oracle.metrics/libs/java_statsd_client-0.0.3-jar-with-dependencies.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/* 2 | sample/ 3 | status/ 4 | .DS_Store 5 | .classpath 6 | .project 7 | .pydevproject 8 | .settings/ 9 | jmx-collector.log 10 | jmx-collector/bin/ 11 | jmx-collector/target/ 12 | *.pyc 13 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/ThresholdDefinition.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | public abstract class ThresholdDefinition { 4 | } 5 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/VirtualMachineDefinition.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | public abstract class VirtualMachineDefinition { 4 | } 5 | -------------------------------------------------------------------------------- /af.oracle.metrics/mvn_inst_local_repo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mvn install:install-file -DgroupId=com.appfirst -DartifactId=java_statsd_client -Dversion=0.3 -Dpackaging=jar -Dfile=libs/java_statsd_client-0.0.3-jar-with-dependencies.jar -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/NoThresholdDefinition.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | public class NoThresholdDefinition extends ThresholdDefinition { 4 | } 5 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/ResultData.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public abstract class ResultData { 4 | @Override 5 | public abstract String toString(); 6 | } 7 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/output/Output.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.output; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.Result; 4 | 5 | public interface Output { 6 | void write(Result result); 7 | } 8 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/MBeanDataConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public interface MBeanDataConverter { 4 | ResultData convert(T type, V value) throws UnsupportedDataTypeException; 5 | } -------------------------------------------------------------------------------- /af.oracle.metrics/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Feb 12 07:27:42 CST 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-bin.zip 7 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/converter/SimpleTypeToStringConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result.converter; 2 | 3 | class SimpleTypeToStringConverter { 4 | String convert(T value) { 5 | return value.toString(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/NoResultData.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public class NoResultData extends ResultData { 4 | @Override 5 | public String toString() { 6 | throw new UnsupportedOperationException(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/OpenMBeanDataConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import javax.management.openmbean.OpenType; 4 | 5 | public interface OpenMBeanDataConverter, V> extends MBeanDataConverter { 6 | } 7 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/CommandLineParserException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | public class CommandLineParserException extends Exception { 4 | public CommandLineParserException(String message) { 5 | super(message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/VirtualMachineIdentifierInvalidationListener.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | public interface VirtualMachineIdentifierInvalidationListener { 4 | void identifierInvalidated(VirtualMachineIdentifier identifier); 5 | } 6 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/UnsupportedDataTypeException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public class UnsupportedDataTypeException extends Exception { 4 | public UnsupportedDataTypeException(String errorMessage) { 5 | super(errorMessage); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /af.oracle.metrics/src/main/java/com/os/appfirst/Metric.java: -------------------------------------------------------------------------------- 1 | package com.os.appfirst; 2 | 3 | public class Metric { 4 | public String name = null; 5 | public long val = 0; 6 | public String unit = null; 7 | 8 | public Metric(String name, long val, String unit){ 9 | this.name = name; 10 | this.val = val; 11 | this.unit = unit; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/ThresholdValidationException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | public class ThresholdValidationException extends CommandValidationException { 4 | public ThresholdValidationException() { 5 | super("Error in threshold definition"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/parser/CommandLineParser.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config.parser; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.config.CommandLineParserException; 4 | 5 | public interface CommandLineParser { 6 | void parse(String commandLine) throws CommandLineParserException; 7 | } 8 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/CommandLinesSource.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | 6 | public interface CommandLinesSource { 7 | List readLines() throws IOException; 8 | 9 | boolean hasChanges() throws IOException; 10 | } 11 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/VirtualMachineResolverRuntimeException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | public class VirtualMachineResolverRuntimeException extends RuntimeException { 4 | public VirtualMachineResolverRuntimeException(String s, Throwable throwable) { 5 | super(s, throwable); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/execution/CommandExecutor.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.execution; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.Command; 4 | import com.objectstyle.appfirst.jmx.collector.result.Result; 5 | 6 | public interface CommandExecutor { 7 | Result execute(Command command) throws CommandExecutionException; 8 | } 9 | -------------------------------------------------------------------------------- /jmx-collector/jmx-collector.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ ! $JAVA_HOME ] 4 | then 5 | echo 'Please specify JAVA_HOME environment variable' 6 | exit 7 | fi 8 | 9 | 10 | LD_LIBRARY_PATH=/usr/share/appfirst:$JAVA_HOME/jre/lib ${JAVA_HOME}/bin/java -cp .:${JAVA_HOME}/lib/jconsole.jar:${JAVA_HOME}/lib/tools.jar:appfirst-jmx-0.4-jar-with-dependencies.jar com.objectstyle.appfirst.jmx.collector.Application 11 | 12 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/VirtualMachineResolver.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.VirtualMachineDefinition; 4 | 5 | public interface VirtualMachineResolver { 6 | VirtualMachineIdentifier resolve(VirtualMachineDefinition definition) throws VirtualMachineResolverException; 7 | } 8 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/output/SysOutOutput.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.output; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.Result; 4 | 5 | public class SysOutOutput extends QueuedOutput implements Output { 6 | @Override 7 | public void doWrite(Result result) { 8 | System.out.println(result.toString()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/execution/ThresholdChecker.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.execution; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.ThresholdDefinition; 4 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 5 | 6 | public interface ThresholdChecker { 7 | boolean checkReached(ThresholdDefinition thresholdDefinition, ResultData resultData); 8 | } 9 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/CommandValidationException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | public class CommandValidationException extends Exception { 4 | public CommandValidationException(String s) { 5 | super(s); 6 | } 7 | 8 | public CommandValidationException(String s, Throwable throwable) { 9 | super(s, throwable); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/VirtualMachineMatcher.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.VirtualMachineDefinition; 4 | import com.sun.tools.attach.VirtualMachineDescriptor; 5 | 6 | public interface VirtualMachineMatcher { 7 | boolean matches(VirtualMachineDefinition definition, VirtualMachineDescriptor descriptor); 8 | } 9 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/VirtualMachineResolverException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | public class VirtualMachineResolverException extends Exception { 4 | public VirtualMachineResolverException(String s) { 5 | super(s); 6 | } 7 | 8 | public VirtualMachineResolverException(String s, Throwable throwable) { 9 | super(s, throwable); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/ValidationErrorResult.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.config.CommandValidationException; 4 | 5 | public class ValidationErrorResult extends Result { 6 | public ValidationErrorResult(CommandValidationException exception) { 7 | super(exception.getMessage(), ResultStatus.EXECUTION_ERROR, new NoResultData()); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /af.oracle.metrics/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Direct log messages to a log file 2 | log4j.appender.file=org.apache.log4j.RollingFileAppender 3 | log4j.appender.file.File= /tmp/af.oracle.metrics.debug.log 4 | log4j.appender.file.MaxFileSize=1MB 5 | log4j.appender.file.MaxBackupIndex=1 6 | log4j.appender.file.layout=org.apache.log4j.PatternLayout 7 | log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 8 | 9 | # Root logger option 10 | log4j.rootLogger=debug, file -------------------------------------------------------------------------------- /af.oracle.metrics/.springBeans: -------------------------------------------------------------------------------- 1 | 2 | 3 | 1 4 | 5 | 6 | 7 | 8 | 9 | 10 | java:com.os.appfirst.App 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/ParseErrorResult.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.config.CommandLineParserException; 4 | 5 | public class ParseErrorResult extends Result { 6 | public ParseErrorResult(CommandLineParserException exception) { 7 | super("JMX-Collector-Parser", ResultStatus.EXECUTION_ERROR, new ErrorResultData(exception.getMessage())); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/UnsupportedOpenTypeException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import javax.management.openmbean.OpenType; 4 | 5 | import static java.lang.String.format; 6 | 7 | public class UnsupportedOpenTypeException extends UnsupportedDataTypeException { 8 | public UnsupportedOpenTypeException(OpenType type) { 9 | super(format("Unsupported data type '%s'", type.getTypeName())); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/ErrorResultData.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public class ErrorResultData extends ResultData { 4 | private final String message; 5 | 6 | public ErrorResultData(String message) { 7 | this.message = message; 8 | } 9 | 10 | public String getMessage() { 11 | return message; 12 | } 13 | 14 | @Override 15 | public String toString() { 16 | return message; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jmx-collector/src/test/java/com/objectstyle/appfirst/jmx/collector/result/SimpleResultDataTest.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import org.junit.Test; 4 | 5 | import static junit.framework.Assert.assertEquals; 6 | 7 | public class SimpleResultDataTest { 8 | @Test 9 | public void testToString() throws Exception { 10 | assertEquals("val=1", new SimpleResultData("val", "1").toString()); 11 | assertEquals("key=2", new SimpleResultData("key", "2").toString()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/ErrorResult.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public class ErrorResult extends Result { 4 | public ErrorResult(String name, String message) { 5 | this(name, new ErrorResultData(message)); 6 | } 7 | 8 | public ErrorResult(String name, ErrorResultData errorResultData) { 9 | super(name, ResultStatus.EXECUTION_ERROR, errorResultData); 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | return super.toString(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jmx-collector/src/test/java/com/objectstyle/appfirst/jmx/collector/result/CompositeResultDataTest.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import org.junit.Test; 5 | 6 | import static junit.framework.Assert.assertEquals; 7 | 8 | public class CompositeResultDataTest { 9 | @Test 10 | public void testToString() throws Exception { 11 | assertEquals("key1=1 key2=2 key3=3", 12 | new CompositeResultData(ImmutableMap.of("key1", "1", "key2", "2", "key3", "3")).toString()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/execution/CommandExecutionException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.execution; 2 | 3 | public class CommandExecutionException extends Exception { 4 | public CommandExecutionException(String s) { 5 | super(s); 6 | } 7 | 8 | public CommandExecutionException(String s, Throwable throwable) { 9 | super(s, throwable); 10 | } 11 | 12 | public CommandExecutionException(Throwable throwable) { 13 | super(String.format("Error while executing command"), throwable); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/VirtualMachineNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.VirtualMachineDefinition; 4 | 5 | import static java.lang.String.format; 6 | 7 | public class VirtualMachineNotFoundException extends VirtualMachineResolverException { 8 | public VirtualMachineNotFoundException(VirtualMachineDefinition definition) { 9 | super(format("Virtual machine definition does not match any virtual machine: '%s'", definition.toString())); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jmx-collector/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=OFF, console, file 2 | 3 | log4j.appender.console=org.apache.log4j.ConsoleAppender 4 | log4j.appender.console.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.console.target=System.err 6 | log4j.appender.console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n 7 | 8 | log4j.appender.file=org.apache.log4j.RollingFileAppender 9 | log4j.appender.file.File=jmx-collector.log 10 | log4j.appender.file.layout=org.apache.log4j.PatternLayout 11 | log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n 12 | 13 | log4j.logger.com.objectstyle.appfirst.jmx.collector=OFF 14 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/NotUniqueVirtualMachineException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.VirtualMachineDefinition; 4 | 5 | import static java.lang.String.format; 6 | 7 | public class NotUniqueVirtualMachineException extends VirtualMachineResolverException { 8 | public NotUniqueVirtualMachineException(VirtualMachineDefinition definition) { 9 | super(format("Virtual machine definition matches more then one virtual machine: '%s'", definition.toString())); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jmx-collector/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=DEBUG, console, file 2 | 3 | log4j.appender.console=org.apache.log4j.ConsoleAppender 4 | log4j.appender.console.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.console.target=System.err 6 | log4j.appender.console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n 7 | 8 | log4j.appender.file=org.apache.log4j.RollingFileAppender 9 | log4j.appender.file.File=jmx-collector.log 10 | log4j.appender.file.layout=org.apache.log4j.PatternLayout 11 | log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n 12 | 13 | log4j.logger.com.objectstyle.appfirst.jmx.collector=TRACE -------------------------------------------------------------------------------- /jmx-collector/src/main/java/TestDeamond.java: -------------------------------------------------------------------------------- 1 | import java.util.Properties; 2 | 3 | public class TestDeamond { 4 | 5 | public static void main(String[] args) { 6 | 7 | Runnable runnable = new Runnable() { 8 | @Override 9 | public void run() { 10 | while(true) 11 | { 12 | Properties properties = System.getProperties(); 13 | properties.list(System.out); 14 | try { 15 | Thread.sleep(1000); 16 | } catch (InterruptedException e) { 17 | break; 18 | } 19 | } 20 | } 21 | }; 22 | 23 | Thread thread = new Thread(runnable); 24 | thread.setDaemon(true); 25 | thread.run(); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/execution/UnsupportedObjectTypeException.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.execution; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.ValueDefinition; 4 | 5 | public class UnsupportedObjectTypeException extends CommandExecutionException { 6 | public UnsupportedObjectTypeException(ValueDefinition valueDefinition, Class objectType) { 7 | super(String.format("Unsupported object type '%s' for attribute '%s' of object '%s'", 8 | objectType.getName(), valueDefinition.getAttribute(), valueDefinition.getName())); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/FileReaderInputSupplier.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import com.google.common.io.InputSupplier; 4 | 5 | import java.io.File; 6 | import java.io.FileReader; 7 | import java.io.IOException; 8 | 9 | public class FileReaderInputSupplier implements InputSupplier { 10 | private final File file; 11 | 12 | public FileReaderInputSupplier(File file) { 13 | this.file = file; 14 | } 15 | 16 | @Override 17 | public Readable getInput() throws IOException { 18 | return new FileReader(file); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/ResultProcessor.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import javax.management.MBeanAttributeInfo; 4 | import javax.management.openmbean.OpenType; 5 | 6 | public interface ResultProcessor { 7 | void startResult(String outputName); 8 | 9 | void startExecutionError(String executionErrorMessage); 10 | 11 | void startData(MBeanAttributeInfo attributeInfo, Object value) throws UnsupportedDataTypeException; 12 | 13 | void startOpenMBeanData(OpenType type, T value) throws UnsupportedOpenTypeException; 14 | 15 | void endResult(); 16 | } 17 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/converter/ToStringConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result.converter; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.MBeanDataConverter; 4 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 5 | import com.objectstyle.appfirst.jmx.collector.result.SimpleResultData; 6 | 7 | public class ToStringConverter implements MBeanDataConverter { 8 | @Override 9 | public ResultData convert(String type, Object value) { 10 | return new SimpleResultData(new SimpleTypeToStringConverter().convert(value)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jmx-collector/src/test/java/com/objectstyle/appfirst/jmx/collector/result/ArrayResultDataTest.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | import org.junit.Test; 5 | 6 | import static junit.framework.Assert.assertEquals; 7 | 8 | public class ArrayResultDataTest { 9 | @Test 10 | public void testToString() throws Exception { 11 | ArrayResultData data = new ArrayResultData("val", ImmutableList.of("1", "2", "3")); 12 | assertEquals("val0=1 val1=2 val2=3", data.toString()); 13 | // yes. we are calling it two times... 14 | assertEquals("val0=1 val1=2 val2=3", data.toString()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /af.oracle.metrics/get_oracle_metric.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | afPath="target" 4 | dbUrl="jdbc:oracle:thin:@192.168.1.66:1521:oracle" 5 | dbUser="orc_test" 6 | dbPass="7012296AA" 7 | logPath="af.oracle.metrics.log" 8 | metricName="cpu" 9 | #metricName="sqls" 10 | interval="5" 11 | 12 | ##java -jar target/af.oracle.metrics-0.1.0-jar-with-dependencies.jar 13 | java -jar $afPath/af.oracle.metrics-0.1.0-jar-with-dependencies.jar -I $interval -D $dbUrl -L $logPath -U $dbUser -P $dbPass -M $metricName 14 | 15 | 16 | #command[oracle.io] java -jar /home/appfirst/af.oracle/af.oracle.metrics-0.1.0-jar-with-dependencies.jar -D "jdbc:oracle:thin:@10.53.55.206:1521:ACRTEST" -U "hr" -P "AcrTek08" -M "custom" -N "User Transaction Per Sec" 17 | 18 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/CommandLineProcessor.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | public interface CommandLineProcessor { 4 | void startCommand(String commandName); 5 | 6 | void endCommand(); 7 | 8 | void startVirtualMachineMatchPattern(String virtualMachineMatchPattern); 9 | 10 | void startVirtualMachineURL(String virtualMachineURL); 11 | 12 | void startMBeanName(String mBeanName); 13 | 14 | void startMBeanAttribute(String mBeanAttribute); 15 | 16 | void startMBeanAttributeKey(String mBeanAttributeKey); 17 | 18 | void startWarningThreshold(String value); 19 | 20 | void startCriticalThreshold(String value); 21 | } 22 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/Constants.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | public interface Constants { 4 | String COMMAND_KEYWORD = "jmx_command"; 5 | String ATTRIBUTE_KEY_PREFIX = "-"; 6 | String VIRTUAL_MACHINE_MATCH_PATTERN_KEY = ATTRIBUTE_KEY_PREFIX + "P"; 7 | String MBEAN_NAME_KEY = ATTRIBUTE_KEY_PREFIX + "O"; 8 | String MBEAN_ATTRIBUTE_KEY = ATTRIBUTE_KEY_PREFIX + "A"; 9 | String MBEAN_ATTRIBUTE_KEY_KEY = ATTRIBUTE_KEY_PREFIX + "K"; 10 | String VIRTUAL_MACHINE_URL_KEY = ATTRIBUTE_KEY_PREFIX + "U"; 11 | String WARNING_THRESHOLD_KEY = ATTRIBUTE_KEY_PREFIX + "W"; 12 | String CRITICAL_THRESHOLD_KEY = ATTRIBUTE_KEY_PREFIX + "C"; 13 | } 14 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/SimpleResultData.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public class SimpleResultData extends ResultData { 4 | public static final String DEFAULT_KEY = "val"; 5 | 6 | private final String key; 7 | 8 | private final String value; 9 | 10 | public SimpleResultData(String key, String value) { 11 | this.key = key; 12 | this.value = value; 13 | } 14 | public SimpleResultData(String value) { 15 | this(DEFAULT_KEY, value); 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return String.format("%s=%s", key, value); 21 | } 22 | 23 | public String getValue() { 24 | return value; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/ResultStatus.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public enum ResultStatus { 4 | REGULAR(0, "OK"), 5 | WARNING_THRESHOLD(1, "Warning"), 6 | CRITICAL_THRESHOLD(2, "Critical"), 7 | EXECUTION_ERROR(3, "Unknown"); 8 | 9 | private final int nagiosIntValue; 10 | 11 | private final String nagiosStringValue; 12 | 13 | private ResultStatus(int nagiosIntValue, String nagiosStringValue) { 14 | this.nagiosIntValue = nagiosIntValue; 15 | this.nagiosStringValue = nagiosStringValue; 16 | } 17 | 18 | public int getNagiosIntValue() { 19 | return nagiosIntValue; 20 | } 21 | 22 | public String getNagiosStringValue() { 23 | return nagiosStringValue; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /poll2log: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Simple script to capture the output of a command, and write it to a logfile. 4 | # The existing file with the given name will be renamed to .old. This will 5 | # cause the AppFirst collector to think that the log file has been "rotated", 6 | # and therefore start reading from the beginning. The point of all this is to 7 | # capture any command output to a log file, while consuming neglible local 8 | # disk space. 9 | # 10 | # This script was designed to be run in a cron job. 11 | 12 | if [ $# != 2 ] 13 | then 14 | echo Usage: $0 command filename 15 | exit 16 | fi 17 | 18 | CMD="$1" 19 | LOGFILE="$2" 20 | OLDLOG="$LOGFILE.old" 21 | 22 | # echo cmd: $CMD, logfile: $LOGFILE, oldlog:$OLDLOG 23 | 24 | if [ -e $LOGFILE ] 25 | then 26 | mv $LOGFILE $OLDLOG 27 | fi 28 | $CMD > $LOGFILE 29 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/ValueDefinition.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | import com.google.common.base.Objects; 4 | 5 | public class ValueDefinition { 6 | private String name; 7 | 8 | private String attribute; 9 | 10 | public ValueDefinition(String name, String attribute) { 11 | this.name = name; 12 | this.attribute = attribute; 13 | } 14 | 15 | public String getName() { 16 | return name; 17 | } 18 | 19 | public String getAttribute() { 20 | return attribute; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return Objects.toStringHelper(this) 26 | .add("name", name) 27 | .add("attribute", attribute) 28 | .toString(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/URLVirtualMachineResolver.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.URLVirtualMachineDefinition; 4 | import com.objectstyle.appfirst.jmx.collector.command.VirtualMachineDefinition; 5 | 6 | public class URLVirtualMachineResolver implements VirtualMachineResolver { 7 | @Override 8 | public VirtualMachineIdentifier resolve(VirtualMachineDefinition definition) 9 | throws VirtualMachineResolverException { 10 | if (definition instanceof URLVirtualMachineDefinition) { 11 | return new VirtualMachineIdentifier(((URLVirtualMachineDefinition) definition).getUrl()); 12 | } 13 | throw new IllegalArgumentException("URL resolver supports URL definitions only"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/URLVirtualMachineDefinition.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | import com.google.common.base.Objects; 4 | 5 | import javax.management.remote.JMXServiceURL; 6 | 7 | public class URLVirtualMachineDefinition extends VirtualMachineDefinition { 8 | 9 | private final JMXServiceURL url; 10 | 11 | public URLVirtualMachineDefinition(JMXServiceURL url) { 12 | this.url = url; 13 | } 14 | 15 | public JMXServiceURL getUrl() { 16 | return url; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return Objects.toStringHelper(this) 22 | .add("url", url) 23 | .toString(); 24 | } 25 | 26 | @Override 27 | public int hashCode() { 28 | return Objects.hashCode(url); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/PatternVirtualMachineDefinition.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | import com.google.common.base.Objects; 4 | 5 | public class PatternVirtualMachineDefinition extends VirtualMachineDefinition { 6 | private final String matchPattern; 7 | 8 | public PatternVirtualMachineDefinition(String matchPattern) { 9 | this.matchPattern = matchPattern; 10 | } 11 | 12 | public String getMatchPattern() { 13 | return matchPattern; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return Objects.toStringHelper(this) 19 | .add("matchPattern", matchPattern) 20 | .toString(); 21 | } 22 | 23 | @Override 24 | public int hashCode() { 25 | return Objects.hashCode(matchPattern); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/CompositeValueDefinition.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | import com.google.common.base.Objects; 4 | 5 | public class CompositeValueDefinition extends ValueDefinition { 6 | private String attributeKey; 7 | 8 | public CompositeValueDefinition(String name, String attribute, String attributeKey) { 9 | super(name, attribute); 10 | this.attributeKey = attributeKey; 11 | } 12 | 13 | public String getAttributeKey() { 14 | return attributeKey; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return Objects.toStringHelper(this) 20 | .add("name", getName()) 21 | .add("attribute", getAttribute()) 22 | .add("attributeKey", attributeKey) 23 | .toString(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/execution/DelegatingThresholdChecker.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.execution; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.NoThresholdDefinition; 4 | import com.objectstyle.appfirst.jmx.collector.command.ThresholdDefinition; 5 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 6 | 7 | public class DelegatingThresholdChecker implements ThresholdChecker { 8 | private final RangeThresholdChecker rangeThresholdChecker = new RangeThresholdChecker(); 9 | 10 | @Override 11 | public boolean checkReached(ThresholdDefinition thresholdDefinition, ResultData resultData) { 12 | if (thresholdDefinition instanceof NoThresholdDefinition) { 13 | return false; 14 | } 15 | return rangeThresholdChecker.checkReached(thresholdDefinition, resultData); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /test/test_check_resque.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys 7 | import os 8 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 9 | sys.path.append(os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from test_plugin import TestPlugin 13 | from check_resque import ResqueChecker 14 | 15 | class TestResqueChecker(TestPlugin): 16 | def setUp(self): 17 | self.checker = ResqueChecker() 18 | try: 19 | os.mkdir("./status/") 20 | except OSError: 21 | pass 22 | print 'check_resque' 23 | 24 | def test_get_connections_active(self): 25 | self.assert_status("-t QUEUE_LENGTH -z resque_test -d ./status/") 26 | 27 | def test_get_connections_waiting(self): 28 | self.assert_status("-t JOB_PROCESSED -z resque_test -d ./status/") 29 | 30 | 31 | if __name__ == "__main__": 32 | unittest.main() -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/converter/SimpleTypeConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result.converter; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.OpenMBeanDataConverter; 4 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 5 | import com.objectstyle.appfirst.jmx.collector.result.SimpleResultData; 6 | import com.objectstyle.appfirst.jmx.collector.result.UnsupportedOpenTypeException; 7 | 8 | import javax.management.openmbean.SimpleType; 9 | 10 | public class SimpleTypeConverter implements OpenMBeanDataConverter, V> { 11 | private SimpleTypeToStringConverter toStringConverter = new SimpleTypeToStringConverter(); 12 | 13 | @Override 14 | public ResultData convert(SimpleType type, V value) throws UnsupportedOpenTypeException { 15 | return new SimpleResultData(toStringConverter.convert(value)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/RangeThresholdDefinition.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | public class RangeThresholdDefinition extends ThresholdDefinition { 4 | private final long leftBorder; 5 | 6 | private final long rightBorder; 7 | 8 | private final boolean inside; 9 | 10 | public RangeThresholdDefinition(long leftBorder, long rightBorder) { 11 | this(leftBorder, rightBorder, false); 12 | } 13 | 14 | public RangeThresholdDefinition(long leftBorder, long rightBorder, boolean inside) { 15 | this.leftBorder = leftBorder; 16 | this.rightBorder = rightBorder; 17 | this.inside = inside; 18 | } 19 | 20 | public long getLeftBorder() { 21 | return leftBorder; 22 | } 23 | 24 | public long getRightBorder() { 25 | return rightBorder; 26 | } 27 | 28 | public boolean isInside() { 29 | return inside; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /af.oracle.metrics/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | maven { url "http://repo.spring.io/libs-snapshot" } 4 | mavenLocal() 5 | } 6 | dependencies { 7 | classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RELEASE") 8 | } 9 | } 10 | 11 | apply plugin: 'java' 12 | apply plugin: 'eclipse-wtp' 13 | apply plugin: 'idea' 14 | apply plugin: 'spring-boot' 15 | apply plugin: 'war' 16 | 17 | war { 18 | baseName = 'gs-convert-jar-to-war' 19 | version = '0.1.0' 20 | } 21 | 22 | repositories { 23 | mavenCentral() 24 | maven { url "http://repo.spring.io/libs-snapshot" } 25 | } 26 | 27 | configurations { 28 | providedRuntime 29 | } 30 | 31 | dependencies { 32 | compile("org.springframework.boot:spring-boot-starter-thymeleaf") 33 | providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") 34 | testCompile("junit:junit") 35 | } 36 | 37 | task wrapper(type: Wrapper) { 38 | gradleVersion = '1.11' 39 | } 40 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/StringInclusionVirtualMachineMatcher.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.PatternVirtualMachineDefinition; 4 | import com.objectstyle.appfirst.jmx.collector.command.VirtualMachineDefinition; 5 | import com.sun.tools.attach.VirtualMachineDescriptor; 6 | 7 | public abstract class StringInclusionVirtualMachineMatcher implements VirtualMachineMatcher { 8 | protected abstract String getVirtualMachineString(VirtualMachineDescriptor descriptor); 9 | 10 | @Override 11 | public boolean matches(VirtualMachineDefinition definition, VirtualMachineDescriptor descriptor) { 12 | if (definition instanceof PatternVirtualMachineDefinition) { 13 | return getVirtualMachineString(descriptor) 14 | .contains(((PatternVirtualMachineDefinition) definition).getMatchPattern()); 15 | } 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test/test_check_passenger.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys 7 | import os 8 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 9 | sys.path.append(os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from test_plugin import TestPlugin 13 | from check_passenger import PassengerChecker 14 | 15 | class TestPassengerChecker(TestPlugin): 16 | def setUp(self): 17 | self.checker = PassengerChecker() 18 | try: 19 | os.mkdir("./status/") 20 | except OSError: 21 | pass 22 | print 'check_passenger' 23 | 24 | def test_get_max_procs(self): 25 | self.assert_status("-t MAX_PROCESSES -z passenger_test -d ./status/") 26 | 27 | def test_get_procs(self): 28 | self.assert_status("-t RUNNING_PROCESSES -z passenger_test -d ./status/") 29 | 30 | def test_get_active_procs(self): 31 | self.assert_status("-t ACTIVE_PROCESSES -z passenger_test -d ./status/") 32 | 33 | if __name__ == "__main__": 34 | unittest.main() -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/FileCommandLinesSource.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.util.List; 6 | 7 | public class FileCommandLinesSource extends SimpleCommandLinesSource { 8 | private final File file; 9 | 10 | private long lastModified = -1; 11 | 12 | public FileCommandLinesSource(File file) { 13 | super(new FileReaderInputSupplier(file)); 14 | this.file = file; 15 | } 16 | 17 | @Override 18 | public boolean hasChanges() throws IOException { 19 | long newLastModified = file.lastModified(); 20 | if (lastModified < newLastModified) { 21 | lastModified = newLastModified; 22 | return true; 23 | } 24 | return false; 25 | } 26 | 27 | @Override 28 | public List readLines() throws IOException { 29 | lastModified = file.lastModified(); 30 | return super.readLines(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/converter/PrimitiveToStringTransformer.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result.converter; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.Primitives; 4 | import org.apache.commons.collections.Transformer; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | public class PrimitiveToStringTransformer implements Transformer { 9 | private static final Logger LOGGER = LoggerFactory.getLogger(PrimitiveToStringTransformer.class); 10 | 11 | private final String errorMessage; 12 | 13 | public PrimitiveToStringTransformer(String errorMessage) { 14 | this.errorMessage = errorMessage; 15 | } 16 | 17 | @Override 18 | public Object transform(Object input) { 19 | if (!Primitives.isPrimitiveOrWrapperType(input.getClass())) { 20 | LOGGER.error(String.format(errorMessage, input.getClass().getName())); 21 | } 22 | return new SimpleTypeToStringConverter().convert(input); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /jmx-collector/config-examples.cfg: -------------------------------------------------------------------------------- 1 | jmx_command[Application-NonHeapMemoryUsage] -P com.objectstyle.appfirst.jmx.collector.Application -O java.lang:type=Memory -A NonHeapMemoryUsage -K used 2 | jmx_command[Application-HeapMemoryUsage] -P com.objectstyle.appfirst.jmx.collector.Application -O java.lang:type=Memory -A HeapMemoryUsage -K used 3 | jmx_command[Applicaiton-Runtime-InputArguments] -P com.objectstyle.appfirst.jmx.collector.Application -O java.lang:type=Runtime -A InputArguments 4 | jmx_command[Applicaiton-Runtime-Name] -P com.objectstyle.appfirst.jmx.collector.Application -O java.lang:type=Runtime -A Name 5 | jmx_command[Application-ObjectPendingFinalizationCount] -P com.objectstyle.appfirst.jmx.collector.Application -O java.lang:type=Memory -A ObjectPendingFinalizationCount -C 5:10 6 | #jmx_command[Cassandra-NonHeapMemory] -U service:jmx:rmi:///jndi/rmi://localhost:7199/jmxrmi -O java.lang:type=Memory -A NonHeapMemoryUsage -W 1000 7 | #jmx_command[Cassandra-NonHeapMemory-error] -U service:jmx:rmi:///jndi/rmi://localhost:7199/jmxrmi -O java.lang:type=Memory -A NonHeapMemoryUsag -W 1000 -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/converter/MapConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result.converter; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.CompositeResultData; 4 | import com.objectstyle.appfirst.jmx.collector.result.MBeanDataConverter; 5 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 6 | import org.apache.commons.collections.MapUtils; 7 | 8 | import java.util.Map; 9 | 10 | public class MapConverter implements MBeanDataConverter { 11 | @Override 12 | @SuppressWarnings("unchecked") 13 | public ResultData convert(String type, Map value) { 14 | Map result = MapUtils.transformedMap( 15 | value, 16 | new PrimitiveToStringTransformer("Unsupported data type '%s' for key inside java.lang.Map type"), 17 | new PrimitiveToStringTransformer("Unsupported data type '%s' for value inside java.lang.Map type") 18 | ); 19 | return new CompositeResultData(result); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/ArrayResultData.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import com.google.common.base.Function; 4 | import com.google.common.base.Joiner; 5 | import com.google.common.collect.Maps; 6 | 7 | import javax.annotation.Nullable; 8 | import java.util.List; 9 | 10 | public class ArrayResultData extends ResultData { 11 | private final String keyPrefix; 12 | 13 | private final List values; 14 | 15 | public ArrayResultData(String keyPrefix, List values) { 16 | this.keyPrefix = keyPrefix; 17 | this.values = values; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return Joiner.on(" ").withKeyValueSeparator("=").join(Maps.uniqueIndex(values, new Function() { 23 | private int keyIndex = 0; 24 | 25 | @Override 26 | public String apply(@Nullable String input) { 27 | return String.format("%s%d", keyPrefix, keyIndex++); 28 | } 29 | })); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Readme_disk_throughput: -------------------------------------------------------------------------------- 1 | Description: 2 | This is a Nagios plugin command, using the nrpe interface, to return information describing 3 | disk throughput. It displays the amount of time the system is busy with the disk as a percentage 4 | of CPU time. It also displays the average queue time im microseconds. 5 | 6 | Build: 7 | % gcc -o disk_throughput disk_throughput.c 8 | 9 | Install: 10 | Copy the executable disk_throughput to the nagios install dir under libexec 11 | Example: 12 | % cp disk_throughput /usr/local/nagios/libexec/. 13 | 14 | Configure: 15 | Add the following command to the nrpe config file in the nagios install dir 16 | under the etc subdir: /usr/local/nagios/etc/nrpe.cfg. The -w param is 17 | the warning threshold and -c is the critical threshold. Linux docs say 18 | that a system value of 70% is critical. 19 | 20 | command[disk_throughput]=/usr/local/nagios/libexec/disk_throughput -w 50 -c 70 21 | 22 | Start/Enable: 23 | Re-start the collector on the system from which disk information is collected 24 | Example: 25 | % sudo /etc/init.d/afcollector restart 26 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/Result.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public class Result { 4 | private final String name; 5 | 6 | private final ResultStatus status; 7 | 8 | private final ResultData data; 9 | 10 | public Result(String name, ResultStatus status, ResultData data) { 11 | this.name = name; 12 | this.status = status; 13 | this.data = data; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public ResultStatus getStatus() { 21 | return status; 22 | } 23 | 24 | public ResultData getData() { 25 | return data; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | StringBuilder stringBuilder = new StringBuilder(); 31 | stringBuilder.append(String.format("%s %s", name, status.getNagiosStringValue())); 32 | if (data instanceof ErrorResultData) { 33 | stringBuilder.append(": ").append(data.toString()); 34 | } else if (!(data instanceof NoResultData)) { 35 | stringBuilder.append(" | ").append(data.toString()); 36 | } 37 | return stringBuilder.toString(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/OpenMBeanDataConverterFactory.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.converter.ArrayTypeConverter; 4 | import com.objectstyle.appfirst.jmx.collector.result.converter.CompositeTypeConverter; 5 | import com.objectstyle.appfirst.jmx.collector.result.converter.SimpleTypeConverter; 6 | 7 | import javax.management.openmbean.ArrayType; 8 | import javax.management.openmbean.CompositeType; 9 | import javax.management.openmbean.OpenType; 10 | import javax.management.openmbean.SimpleType; 11 | 12 | public enum OpenMBeanDataConverterFactory { 13 | INSTANCE; 14 | 15 | @SuppressWarnings("unchecked") 16 | public OpenMBeanDataConverter getConverter(OpenType type) 17 | throws UnsupportedOpenTypeException { 18 | if (type instanceof ArrayType) { 19 | return new ArrayTypeConverter(); 20 | } else if (type instanceof SimpleType) { 21 | return new SimpleTypeConverter(); 22 | } else if (type instanceof CompositeType) { 23 | return new CompositeTypeConverter(); 24 | } else { 25 | throw new UnsupportedOpenTypeException(type); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/JvmstatVirtualMachineMatcher.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.sun.tools.attach.VirtualMachineDescriptor; 4 | import sun.jvmstat.monitor.*; 5 | 6 | import java.net.URISyntaxException; 7 | 8 | public class JvmstatVirtualMachineMatcher extends StringInclusionVirtualMachineMatcher implements VirtualMachineMatcher { 9 | @Override 10 | protected String getVirtualMachineString(VirtualMachineDescriptor descriptor) { 11 | try { 12 | MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(new HostIdentifier((String) null)); 13 | MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(new VmIdentifier(descriptor.id())); 14 | return MonitoredVmUtil.jvmArgs(monitoredVm) + " " 15 | + MonitoredVmUtil.jvmFlags(monitoredVm) + " " + MonitoredVmUtil.commandLine(monitoredVm); 16 | } catch (MonitorException e) { 17 | throw new VirtualMachineResolverRuntimeException("Error while trying while getting VM attributes through Monitor API", e); 18 | } catch (URISyntaxException e) { 19 | throw new IllegalArgumentException("Can't create VM identifier from provided VM descriptor"); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/SimpleCommandLinesSource.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import com.google.common.io.InputSupplier; 4 | import com.google.common.io.LineReader; 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | import java.io.IOException; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | public class SimpleCommandLinesSource implements CommandLinesSource { 12 | private final InputSupplier inputSupplier; 13 | 14 | public SimpleCommandLinesSource(InputSupplier inputSupplier) { 15 | this.inputSupplier = inputSupplier; 16 | } 17 | 18 | @Override 19 | public List readLines() throws IOException { 20 | LineReader reader = new LineReader(inputSupplier.getInput()); 21 | List lines = new ArrayList(); 22 | String line; 23 | while ((line = reader.readLine()) != null) { 24 | String striped = StringUtils.strip(line); 25 | if (striped.startsWith(Constants.COMMAND_KEYWORD)) { 26 | lines.add(striped); 27 | } 28 | } 29 | return lines; 30 | } 31 | 32 | @Override 33 | public boolean hasChanges() throws IOException { 34 | return true; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/Primitives.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | public class Primitives { 4 | 5 | public static boolean isPrimitiveOrWrapperType(Class clazz){ 6 | return isPrimitiveType(clazz) || isWrapperType(clazz); 7 | } 8 | 9 | public static boolean isWrapperType(Class clazz) { 10 | return clazz.equals(Boolean.class) || 11 | clazz.equals(Integer.class) || 12 | clazz.equals(Character.class) || 13 | clazz.equals(Byte.class) || 14 | clazz.equals(Short.class) || 15 | clazz.equals(Double.class) || 16 | clazz.equals(Long.class) || 17 | clazz.equals(Void.class) || 18 | clazz.equals(String.class) || 19 | clazz.equals(Float.class); 20 | } 21 | 22 | public static boolean isPrimitiveType(Class clazz) { 23 | return clazz.equals(boolean.class) || 24 | clazz.equals(int.class) || 25 | clazz.equals(char.class) || 26 | clazz.equals(byte.class) || 27 | clazz.equals(short.class) || 28 | clazz.equals(double.class) || 29 | clazz.equals(long.class) || 30 | clazz.equals(void.class) || 31 | clazz.equals(float.class); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /af.oracle.metrics/src/test/java/com/os/appfirst/OracleMetricsTest.java: -------------------------------------------------------------------------------- 1 | package com.os.appfirst; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | public class OracleMetricsTest { 10 | 11 | private OracleMetrics metrics; 12 | 13 | @Before 14 | public void setUp() throws Exception { 15 | this.metrics = new OracleMetrics(null, null, null); 16 | } 17 | 18 | @After 19 | public void tearDown() throws Exception { 20 | } 21 | 22 | @Test 23 | public void testGetTag() { 24 | String sql = "SELECT COUNT(REGION_ID) FROM REGIONS"; 25 | String tag = this.metrics.getTag(sql); 26 | 27 | assertEquals("tag", ":AF_SQL_Select:", tag); 28 | 29 | 30 | // select 31 | sql = " select LOCATION_ID, CITY, POSTAL_CODE, STATE_PROVINCE, STREET_ADDRESS, COUNTRY_ID FROM LOCATIONS WHERE (LOCATION_ID = :1 )"; 32 | tag = this.metrics.getTag(sql); 33 | assertEquals("tag", ":AF_SQL_Select:", tag); 34 | 35 | 36 | // select 37 | sql = " select LOCATION_ID, CITY, POSTAL_CODE, STATE_PROVINCE, STREET_ADDRESS, COUNTRY_ID FROM LOCATIONS WHERE (LOCATION_ID = :1 ) update"; 38 | tag = this.metrics.getTag(sql); 39 | assertEquals("tag", ":AF_SQL_Select:", tag); 40 | 41 | // -- 42 | sql = " dsfnjwhfkjbdskvbskdbvkjejhqdjklnsv"; 43 | tag = this.metrics.getTag(sql); 44 | assertEquals("tag", ":AF_SQL:", tag); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /apache_log/readme.md: -------------------------------------------------------------------------------- 1 | App First apache access log statistic 2 | ===================== 3 | 4 | A module parse apche access log files and calculates 5 | number of visitited urls and store that data in specifyed file, 6 | by default af_apache_visited_urls.log in folder where script located. 7 | 8 | 9 | * `python af_alogs.py -h` will detail configuration flags 10 | 11 | to start daemon: 12 | `python af_alogs.py start --interval=60 --apache-host=apacheHostName1` 13 | and to stop: 14 | `python af_alogs.py stop` 15 | 16 | to get current state of daemon and count of urls: 17 | `python af_alogs.py status` 18 | 19 | Dependensis: 20 | ----------- 21 | afstatsd 22 | `pip install afstatsd` 23 | 24 | Configuration flags 25 | ----------- 26 | `--interval` - interval in seconds to ask apache access logs about updates, by default 60 sec 27 | 28 | `--apache-log-file-path` - Path to apache log file , by default `/var/log/apache2/access.log` 29 | 30 | `--output-log-file-path` - Path to file where urls count will be saved, by default `af_apache_visited_urls.log` 31 | 32 | `--tags` - Enable tags for log file, should be in format like :AF_TAG_NAME: 33 | `-a --apache-host` - Apache hostname will be included in statsD counter name 34 | 35 | 36 | Example 37 | ----------- 38 | command example to use at "Polled Data Config" 39 | 40 | command[apache_log]=python /usr/share/appfirst/plugins/libexec/apache_log/af_alogs.py status 41 | -------------------------------------------------------------------------------- /test/test_check_redis.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys 7 | import os 8 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 9 | sys.path.append(os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from test_plugin import TestPlugin 13 | from check_redis import RedisChecker 14 | 15 | class TestRedisChecker(TestPlugin): 16 | def setUp(self): 17 | self.checker = RedisChecker() 18 | try: 19 | os.mkdir("./status/") 20 | except OSError: 21 | pass 22 | print 'check_redis' 23 | 24 | def test_get_average_operations_rate(self): 25 | self.assert_status("-t AVERAGE_OPERATIONS_RATE -z redis_test -d ./status/") 26 | 27 | def test_get_current_operations_rate(self): 28 | self.assert_status("-t CURRENT_OPERATIONS -z redis_test -d ./status/") 29 | 30 | def test_get_memory_used(self): 31 | self.assert_status("-t MEMORY_USED -z redis_test -d ./status/") 32 | 33 | def test_get_current_operations(self): 34 | self.assert_status("-t CURRENT_CHANGES -z redis_test -d ./status/") 35 | 36 | def test_get_change_since_last_save(self): 37 | self.assert_status("-t CHANGES_SINCE_LAST_SAVE -z redis_test -d ./status/") 38 | 39 | def test_get_total_keys(self): 40 | self.assert_status("-t TOTAL_KEYS -z redis_test -d ./status/") 41 | 42 | if __name__ == "__main__": 43 | unittest.main() -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/CompositeResultData.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import com.google.common.base.Function; 4 | import com.google.common.base.Joiner; 5 | import com.google.common.collect.ImmutableMap; 6 | import com.google.common.collect.Maps; 7 | 8 | import java.util.Map; 9 | 10 | import static com.google.common.collect.Collections2.transform; 11 | import static java.lang.String.format; 12 | 13 | public class CompositeResultData extends ResultData { 14 | private final Map values; 15 | 16 | public CompositeResultData(Map values) { 17 | this.values = values; 18 | } 19 | 20 | public boolean containsKey(String key) { 21 | return values.containsKey(key); 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return Joiner.on(" ").withKeyValueSeparator("=").join(transform(values.entrySet(), new Function, Map.Entry>() { 27 | @Override 28 | public Map.Entry apply(Map.Entry input) { 29 | return Maps.immutableEntry(format("%s", input.getKey()), input.getValue()); 30 | } 31 | })); 32 | } 33 | 34 | public CompositeResultData forSingleKey(String key) { 35 | return new CompositeResultData(ImmutableMap.of(key, values.get(key))); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /af.oracle.metrics/src/main/java/com/os/appfirst/Log2File.java: -------------------------------------------------------------------------------- 1 | package com.os.appfirst; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.File; 5 | import java.io.FileWriter; 6 | import java.io.IOException; 7 | 8 | import org.apache.log4j.Logger; 9 | 10 | public class Log2File { 11 | static final Logger logger = Logger.getLogger(Log2File.class); 12 | public String outputFileName = null; 13 | private BufferedWriter bw = null; 14 | 15 | public Log2File(String outputFileName){ 16 | this.outputFileName = outputFileName; 17 | 18 | File file = new File(this.outputFileName); 19 | try { 20 | if (!file.exists()) { 21 | file.createNewFile(); 22 | } 23 | 24 | FileWriter fw; 25 | 26 | fw = new FileWriter(file.getAbsoluteFile(), true); 27 | 28 | this.bw = new BufferedWriter(fw); 29 | } catch (IOException e) { 30 | logger.fatal("writeToFile error: " + e.getMessage()); 31 | } 32 | } 33 | 34 | // @TODO close file only when application done his tasks 35 | public void writeToFile(String str) { 36 | 37 | try { 38 | if (this.bw != null) { 39 | this.bw.write(str); 40 | this.bw.newLine(); 41 | } 42 | 43 | } catch (IOException e) { 44 | logger.fatal("writeToFile error: " + e.getMessage()); 45 | } 46 | 47 | } 48 | 49 | public void close() { 50 | try { 51 | if (this.bw != null) { 52 | this.bw.close(); 53 | } 54 | } catch (IOException e) { 55 | logger.fatal("error closing BufferedWriter: " + e.getMessage()); 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /test/test_check_memcached.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys 7 | import os 8 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 9 | sys.path.append(os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from test_plugin import TestPlugin 13 | from check_memcached import MemcachedChecker 14 | 15 | class TestMemcachedChecker(TestPlugin): 16 | def setUp(self): 17 | self.checker = MemcachedChecker() 18 | try: 19 | os.mkdir("./status/") 20 | except OSError: 21 | pass 22 | print 'check_memcached' 23 | 24 | def test_get_cmd_set(self): 25 | self.assert_status("-t OPERATIONS_SET_REQUESTS -z memcached_test -d ./status/") 26 | 27 | def test_get_cmd_get(self): 28 | self.assert_status("-t OPERATIONS_GET_REQUESTS -z memcached_test -d ./status/") 29 | 30 | def test_get_bytes_read(self): 31 | self.assert_status("-t BYTES_READ -z memcached_test -d ./status/") 32 | 33 | def test_get_bytes_written(self): 34 | self.assert_status("-t BYTES_WRITTEN -z memcached_test -d ./status/") 35 | 36 | def test_get_bytes_allocated(self): 37 | self.assert_status("-t BYTES_ALLOCATED -z memcached_test -d ./status/") 38 | 39 | def test_get_total_items(self): 40 | self.assert_status("-t TOTAL_ITEMS -z memcached_test -d ./status/") 41 | 42 | def test_get_current_connections(self): 43 | self.assert_status("-t CURRENT_CONNECTIONS -z memcached_test -d ./status/") 44 | 45 | if __name__ == "__main__": 46 | unittest.main() -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/LocalVirtualMachineIdentifier.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.google.common.base.Objects; 4 | import com.sun.tools.attach.VirtualMachineDescriptor; 5 | 6 | import javax.management.remote.JMXServiceURL; 7 | 8 | public class LocalVirtualMachineIdentifier extends VirtualMachineIdentifier { 9 | private final VirtualMachineDescriptor descriptor; 10 | 11 | public LocalVirtualMachineIdentifier(VirtualMachineDescriptor descriptor, JMXServiceURL url) { 12 | super(url); 13 | this.descriptor = descriptor; 14 | } 15 | 16 | public VirtualMachineDescriptor getDescriptor() { 17 | return descriptor; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return Objects.toStringHelper(this) 23 | .add("url", getUrl()) 24 | .add("descriptor", getDescriptor()) 25 | .toString(); 26 | } 27 | 28 | @Override 29 | public boolean equals(Object o) { 30 | if (this == o) return true; 31 | if (!(o instanceof LocalVirtualMachineIdentifier)) return false; 32 | if (!super.equals(o)) return false; 33 | 34 | LocalVirtualMachineIdentifier that = (LocalVirtualMachineIdentifier) o; 35 | 36 | return descriptor.equals(that.descriptor); 37 | } 38 | 39 | @Override 40 | public int hashCode() { 41 | int result = super.hashCode(); 42 | result = 31 * result + (descriptor != null ? descriptor.hashCode() : 0); 43 | return result; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /ucommands.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 7, 2012 3 | 4 | a thin wrap of subprocess module as an adapter to commands module 5 | 6 | @author: Yangming 7 | ''' 8 | import subprocess 9 | 10 | def getstatusoutput(cmd): 11 | ''' 12 | Execute the string cmd in a shell with subprocess.check_call() and return a 2-tuple (status, output). 13 | cmd is actually run as { cmd ; } 2>&1, so that the returned output will contain output or error messages. 14 | A trailing newline is stripped from the output. 15 | The exit status for the command can be interpreted according to the rules for the C function wait(). 16 | ''' 17 | try: 18 | output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True) 19 | return 0, output 20 | except subprocess.CalledProcessError, e: 21 | return e.returncode, e.output 22 | 23 | def getoutput(cmd): 24 | ''' 25 | Like getstatusoutput(), except the exit status is ignored and the return value is a string containing the command output. 26 | ''' 27 | try: 28 | output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True) 29 | return output 30 | except subprocess.CalledProcessError, e: 31 | return e.output 32 | 33 | def getstatus(filename): 34 | ''' 35 | Return the output of ls -ld file as a string. 36 | This function uses the getoutput() function, and properly escapes backslashes and dollar signs in the argument. 37 | Deprecated since version 2.6: This function is nonobvious and useless. The name is also misleading in the presence of getstatusoutput(). 38 | ''' 39 | f = open(filename, "r") 40 | return getoutput(f) -------------------------------------------------------------------------------- /test/test_check_mysql.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys 7 | import os 8 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 9 | sys.path.append(os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from test_plugin import TestPlugin 13 | from check_mysql import MySqlChecker 14 | 15 | class TestMySqlChecker(TestPlugin): 16 | def setUp(self): 17 | self.checker = MySqlChecker() 18 | try: 19 | os.mkdir("./status/") 20 | except OSError: 21 | pass 22 | print 'check_mysql' 23 | 24 | def test_get_queries_per_second(self): 25 | self.assert_status("-t QUERIES_PER_SECOND -z mysql_test -d ./status/") 26 | 27 | def test_get_slow_queries(self): 28 | self.assert_status("-t SLOW_QUERIES -z mysql_test -d ./status/") 29 | 30 | def test_get_row_opertions(self): 31 | self.assert_status("-t ROW_OPERATIONS -z mysql_test -d ./status/") 32 | 33 | def test_get_transactions(self): 34 | self.assert_status("-t TRANSACTIONS -z mysql_test -d ./status/") 35 | 36 | #def test_get_network_traffic(self, request): 37 | # pass 38 | 39 | def test_get_connections(self): 40 | self.assert_status("-t CONNECTIONS -z mysql_test -d ./status/") 41 | 42 | def test_get_bytes_transfer(self): 43 | self.assert_status("-t TOTAL_BYTES -z mysql_test -d ./status/") 44 | 45 | def test_get_select_stats(self): 46 | self.assert_status("-t SELECTS -z mysql_test -d ./status/") 47 | 48 | #def test_get_replication(self, request): 49 | # pass 50 | 51 | if __name__ == "__main__": 52 | unittest.main() -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/converter/SetConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result.converter; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.CompositeResultData; 4 | import com.objectstyle.appfirst.jmx.collector.result.MBeanDataConverter; 5 | import com.objectstyle.appfirst.jmx.collector.result.Primitives; 6 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 7 | import com.objectstyle.appfirst.jmx.collector.result.SimpleResultData; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | import java.util.Set; 14 | 15 | import static java.lang.String.format; 16 | 17 | public class SetConverter implements MBeanDataConverter { 18 | private static final Logger LOGGER = LoggerFactory.getLogger(SetConverter.class); 19 | 20 | private SimpleTypeToStringConverter toStringConverter = new SimpleTypeToStringConverter(); 21 | 22 | @Override 23 | public ResultData convert(String type, Set value) { 24 | Map compositeValue = new HashMap(value.size()); 25 | 26 | int index = 1; 27 | for (Object item : value) { 28 | if (!Primitives.isPrimitiveOrWrapperType(item.getClass())) { 29 | LOGGER.warn(format("Unsupported data type '%s' inside java.lang.Set type", item.getClass().getName())); 30 | } 31 | compositeValue.put(SimpleResultData.DEFAULT_KEY + index, toStringConverter.convert(item)); 32 | index++; 33 | } 34 | return new CompositeResultData(compositeValue); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /test/test_check_smart.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys 7 | import os 8 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 9 | sys.path.append(os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from test_plugin import TestPlugin 13 | from check_smart import SmartChecker 14 | 15 | class TestSmartChecker(TestPlugin): 16 | def setUp(self): 17 | self.checker = SmartChecker() 18 | try: 19 | os.mkdir("./status/") 20 | except OSError: 21 | pass 22 | print 'check_smart' 23 | 24 | def test_get_overall_health(self): 25 | self.print_status("-t OVERALL_HEALTH -d ./status/") 26 | 27 | def test_get_adapatec_health(self): 28 | self.print_status("-t ADAPTEC_HEALTH -d ./status/") 29 | 30 | def test_get_connections_active(self): 31 | self.print_status("-t REALLOCATE_SECTOR_COUNT -d ./status/") 32 | 33 | def test_get_spin_retry_count(self): 34 | self.print_status("-t SPIN_RETRY_COUNT -d ./status/") 35 | 36 | def test_get_offline_uncorrectable(self): 37 | self.print_status("-t OFFLINE_UNCORRECTABLE -d ./status/") 38 | 39 | def test_get_cur_pending_sector(self): 40 | self.print_status("-t CUR_PENDING_SECTOR -d ./status/") 41 | 42 | def test_get_reallocated_event_count(self): 43 | self.print_status("-t REALLOCATED_EVENT_COUNT -d ./status/") 44 | 45 | def test_get_spin_up_time(self): 46 | self.print_status("-t SPIN_UP_TIME -d ./status/") 47 | 48 | def test_get_raw_read_error_rate(self): 49 | self.print_status("-t RAW_READ_ERROR_RATE -d ./status/") 50 | 51 | if __name__ == "__main__": 52 | unittest.main() -------------------------------------------------------------------------------- /af.oracle.metrics/src/test/java/com/os/appfirst/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.os.appfirst; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.ArrayList; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | 12 | public class AppTest { 13 | ArrayList metrics1 = new ArrayList(); 14 | static NagiosOutput nagiosOut = new NagiosOutput(); 15 | 16 | @Before 17 | public void setUp() throws Exception { 18 | this.metrics1.add( 19 | new Metric( 20 | "METRIC_NAME", 21 | 1, 22 | "METRIC_UNIT" 23 | ) 24 | ); 25 | 26 | this.metrics1.add( 27 | new Metric( 28 | "METRIC_NAME1", 29 | 2, 30 | "METRIC_UNIT" 31 | ) 32 | ); 33 | } 34 | 35 | @After 36 | public void tearDown() throws Exception { 37 | } 38 | 39 | @Test 40 | public void testListOfMetricsAsArgument() { 41 | int argumentsCount = 2; 42 | String strMerics = "specific_metric_name1;specific_metric_name2;"; 43 | String[] arrOfMetrics = strMerics.split(";"); 44 | assertEquals("length", arrOfMetrics.length, argumentsCount); 45 | 46 | for(String s: arrOfMetrics){ 47 | System.out.println("metric: " + s); 48 | ArrayList metrics2 = new ArrayList(); 49 | metrics2.add( 50 | new Metric( 51 | s, 52 | 1, 53 | "METRIC_UNIT" 54 | ) 55 | ); 56 | this.metrics1.addAll(metrics2); 57 | 58 | } 59 | 60 | System.out.println(this.metrics1); 61 | 62 | assertEquals("metrics size", this.metrics1.size(), argumentsCount * 2); 63 | 64 | String msg = nagiosOut.formatMetrics(this.metrics1); 65 | System.out.println(msg); 66 | assertNotNull(msg); 67 | 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /af.oracle.metrics/info.md: -------------------------------------------------------------------------------- 1 | mvn clean package && mvn cargo:redeploy 2 | mvn clean compile assembly:single 3 | 4 | 5 | # AppFirst StatsD 6 | https://github.com/appfirst/statsd_clients/tree/stable/java 7 | 8 | # v$sysstat names 9 | https://sites.google.com/site/oraclemonitor/v-sysstat-names 10 | 11 | ## physical reads 12 | This statistic stores the number of I/O requests to the operating system to retrieve a database block from the disk subsystem. 13 | This is a buffer cache miss. Logical reads is consistent gets + database block gets. Logical reads and physical reads is used 14 | to calculate the buffer cache hit ratio. 15 | 16 | ## physical writes 17 | This statistic stores the number of I/O requests to the operating system to write a database block to the disk subsystem. 18 | The bulk of the writes are performed either by DBWR or LGWR. 19 | 20 | # v$sysmetric_history , v$sysmetric , and v$sysmetric_summary 21 | displays the system metric values captured for the most current time interval for both the 22 | long duration (60-second) and short duration (15-second) system metrics. 23 | 24 | 25 | # v$session 26 | 27 | list of all sessions to identify active sessions with STATUS='ACTIVE' and how long it was running LAST_CALL_ET 28 | see running sql with SQL_ID or SQL_ADDR 29 | If session is waiting check WAIT columns for current wait details. 30 | V$PROCCES is mostly with SESS_ADDR to find process ID of Oracle background process and application process ID. 31 | V$SQLAREA and V$SQL provide text of SQL and more details for each SQL still in cache. 32 | 33 | 34 | #v$sql 35 | ELAPSED_TIME/EXECUTIONS 36 | 37 | 38 | http://www.pythian.com/blog/do-you-know-if-your-database-slow/ -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/execution/RangeThresholdChecker.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.execution; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.RangeThresholdDefinition; 4 | import com.objectstyle.appfirst.jmx.collector.command.ThresholdDefinition; 5 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 6 | import com.objectstyle.appfirst.jmx.collector.result.SimpleResultData; 7 | 8 | public class RangeThresholdChecker implements ThresholdChecker { 9 | @Override 10 | public boolean checkReached(ThresholdDefinition thresholdDefinition, ResultData resultData) { 11 | if (!(thresholdDefinition instanceof RangeThresholdDefinition)) { 12 | throw new IllegalArgumentException("Only range thresholds are supported"); 13 | } 14 | RangeThresholdDefinition rangeThresholdDefinition = (RangeThresholdDefinition) thresholdDefinition; 15 | if (resultData instanceof SimpleResultData) { 16 | try { 17 | long value = Long.valueOf(((SimpleResultData) resultData).getValue()); 18 | if (rangeThresholdDefinition.isInside()) { 19 | return rangeThresholdDefinition.getLeftBorder() <= value 20 | && rangeThresholdDefinition.getRightBorder() >= value; 21 | } else { 22 | return rangeThresholdDefinition.getLeftBorder() > value 23 | || rangeThresholdDefinition.getRightBorder() < value; 24 | } 25 | } catch (NumberFormatException e) { 26 | // non numerical data, ignoring exception 27 | } 28 | } 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/VirtualMachineIdentifier.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.google.common.base.Objects; 4 | 5 | import javax.management.remote.JMXServiceURL; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | public class VirtualMachineIdentifier { 10 | private List listeners; 11 | 12 | private final JMXServiceURL url; 13 | 14 | public VirtualMachineIdentifier(JMXServiceURL url) { 15 | this.url = url; 16 | listeners = new ArrayList(); 17 | } 18 | 19 | public JMXServiceURL getUrl() { 20 | return url; 21 | } 22 | 23 | public void invalidate() { 24 | for (VirtualMachineIdentifierInvalidationListener listener : listeners) { 25 | listener.identifierInvalidated(this); 26 | } 27 | } 28 | 29 | public void addInvalidationListener(VirtualMachineIdentifierInvalidationListener listener) { 30 | if (!listeners.contains(listener)) { 31 | listeners.add(listener); 32 | } 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return Objects.toStringHelper(this) 38 | .add("url", getUrl()) 39 | .toString(); 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | VirtualMachineIdentifier that = (VirtualMachineIdentifier) o; 48 | 49 | return url.equals(that.url); 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | return url != null ? url.hashCode() : 0; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/converter/CompositeTypeConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result.converter; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.CompositeResultData; 4 | import com.objectstyle.appfirst.jmx.collector.result.OpenMBeanDataConverter; 5 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 6 | import com.objectstyle.appfirst.jmx.collector.result.UnsupportedOpenTypeException; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | import javax.management.openmbean.CompositeData; 11 | import javax.management.openmbean.CompositeType; 12 | import javax.management.openmbean.OpenType; 13 | import javax.management.openmbean.SimpleType; 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | 17 | import static java.lang.String.format; 18 | 19 | public class CompositeTypeConverter implements OpenMBeanDataConverter { 20 | private static final Logger LOGGER = LoggerFactory.getLogger(CompositeTypeConverter.class); 21 | 22 | private SimpleTypeToStringConverter toStringConverter = new SimpleTypeToStringConverter(); 23 | 24 | @Override 25 | public ResultData convert(CompositeType type, CompositeData value) throws UnsupportedOpenTypeException { 26 | Map valueMap = new HashMap(); 27 | for (String key : type.keySet()) { 28 | OpenType itemType = type.getType(key); 29 | if (!(itemType instanceof SimpleType)) { 30 | LOGGER.warn(format("Unsupported data type '%s' inside CompositeType bean.", itemType)); 31 | } 32 | valueMap.put(key, toStringConverter.convert(value.get(key))); 33 | } 34 | return new CompositeResultData(valueMap); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /af.oracle.metrics/src/main/java/com/os/appfirst/NagiosOutput.java: -------------------------------------------------------------------------------- 1 | package com.os.appfirst; 2 | 3 | import java.util.ArrayList; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | //Plugin Return Code Service State Host State 8 | //0 OK UP 9 | //1 WARNING UP or DOWN/UNREACHABLE* 10 | //2 CRITICAL DOWN/UNREACHABLE 11 | //3 UNKNOWN DOWN/UNREACHABLE 12 | public class NagiosOutput { 13 | static final Logger logger = Logger.getLogger(NagiosOutput.class); 14 | static final String baseNameOfMetric = "Oracle.DB"; 15 | static final String sep = " : "; 16 | 17 | public NagiosOutput (){ 18 | 19 | } 20 | 21 | public void echoOk(String msg){ 22 | System.out.println(baseNameOfMetric + " OK" + sep + msg); 23 | } 24 | 25 | public void echoWarn(String msg){ 26 | System.out.println(baseNameOfMetric + " WARNING" + sep + msg); 27 | } 28 | 29 | public void echoCritical(String msg){ 30 | System.out.println(baseNameOfMetric + " CRITICAL" + sep + msg); 31 | } 32 | 33 | private String trimName(String name){ 34 | return name.replace("(", "").replace(")", "").replace("%", ""); 35 | } 36 | 37 | private String formatName(String name){ 38 | return trimName(name).replace(" ", "_").toLowerCase(); 39 | } 40 | 41 | public String formatMetrics(ArrayList metrics) { 42 | if (metrics != null){ 43 | String nOutput = ""; 44 | String nPerfData = ""; 45 | 46 | for(Metric m: metrics){ 47 | nOutput += trimName(m.name) + " = " + m.val + ","; 48 | 49 | } 50 | 51 | nOutput = nOutput.substring(0, nOutput.length() - 1); 52 | 53 | for(Metric m: metrics){ 54 | nPerfData += this.formatName(m.name) + "=" + m.val + " "; 55 | } 56 | nPerfData = nPerfData.substring(0, nPerfData.length() - 1); 57 | 58 | return nOutput + " | " + nPerfData; 59 | } else { 60 | return "List of metrics are NULL"; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/converter/ArrayTypeConverter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result.converter; 2 | 3 | import com.google.common.base.Function; 4 | import com.objectstyle.appfirst.jmx.collector.result.ArrayResultData; 5 | import com.objectstyle.appfirst.jmx.collector.result.OpenMBeanDataConverter; 6 | import com.objectstyle.appfirst.jmx.collector.result.ResultData; 7 | import com.objectstyle.appfirst.jmx.collector.result.SimpleResultData; 8 | import com.objectstyle.appfirst.jmx.collector.result.UnsupportedOpenTypeException; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import javax.annotation.Nullable; 13 | import javax.management.openmbean.ArrayType; 14 | import javax.management.openmbean.SimpleType; 15 | 16 | import static com.google.common.collect.Lists.newArrayList; 17 | import static com.google.common.collect.Lists.transform; 18 | import static java.lang.String.format; 19 | 20 | public class ArrayTypeConverter implements OpenMBeanDataConverter, T[]> { 21 | private static final Logger LOGGER = LoggerFactory.getLogger(CompositeTypeConverter.class); 22 | 23 | private SimpleTypeToStringConverter toStringConverter = new SimpleTypeToStringConverter(); 24 | 25 | @Override 26 | public ResultData convert(final ArrayType type, T[] values) throws UnsupportedOpenTypeException { 27 | if (!(type.getElementOpenType() instanceof SimpleType)) { 28 | LOGGER.warn(format("Unsupported data type '%s' inside ArrayType bean.", type.getElementOpenType())); 29 | } 30 | return new ArrayResultData(SimpleResultData.DEFAULT_KEY, transform(newArrayList(values), new Function() { 31 | @Override 32 | public String apply(@Nullable T input) { 33 | return toStringConverter.convert(input); 34 | } 35 | })); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/utils/UserGroupGetter.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.utils; 2 | 3 | import org.apache.commons.exec.*; 4 | import org.apache.commons.io.IOUtils; 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | 8 | import javax.swing.*; 9 | import java.io.*; 10 | import java.util.List; 11 | 12 | public class UserGroupGetter { 13 | 14 | private static final String CMD_LINE = "ps -p %d -o user,group"; 15 | 16 | private int pid; 17 | 18 | private String user; 19 | private String group; 20 | 21 | 22 | public void get() 23 | { 24 | CommandLine cmdLine = CommandLine.parse(String.format(CMD_LINE,getPid())); 25 | ByteArrayOutputStream std = new ByteArrayOutputStream(); 26 | ByteArrayOutputStream err = new ByteArrayOutputStream(); 27 | PumpStreamHandler handler = new PumpStreamHandler(std, err); 28 | DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); 29 | 30 | DefaultExecutor executor = new DefaultExecutor(); 31 | executor.setStreamHandler(handler); 32 | try { 33 | executor.execute(cmdLine,resultHandler); 34 | 35 | resultHandler.waitFor(); 36 | 37 | parse(std.toString()); 38 | } catch (IOException e) { 39 | throw new IllegalArgumentException(e); 40 | } catch (InterruptedException e) 41 | { 42 | e.printStackTrace(); 43 | } 44 | finally { 45 | IOUtils.closeQuietly(std); 46 | IOUtils.closeQuietly(err); 47 | } 48 | } 49 | 50 | private void parse(String output) 51 | { 52 | String[] strings = StringUtils.split(output); 53 | if (strings.length == 4) 54 | { 55 | user = strings[2]; 56 | group = strings[3]; 57 | } 58 | } 59 | 60 | 61 | 62 | 63 | public String getUser() { 64 | return user; 65 | } 66 | 67 | public String getGroup() { 68 | return group; 69 | } 70 | 71 | public int getPid() { 72 | return pid; 73 | } 74 | 75 | public void setPid(int pid) { 76 | this.pid = pid; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /jmx-collector/src/test/java/com/objectstyle/appfirst/jmx/collector/utils/JVMAttachTest.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.utils; 2 | 3 | import org.junit.Test; 4 | import sun.tools.jconsole.LocalVirtualMachine; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | import static org.junit.Assert.assertNotNull; 8 | import static org.mockito.Mockito.mock; 9 | import static org.mockito.Mockito.spy; 10 | import static org.mockito.Mockito.when; 11 | 12 | 13 | public class JVMAttachTest { 14 | 15 | @Test 16 | public void test() throws Exception { 17 | JVMAttach jvmAttach = spy(new JVMAttach()); 18 | UserGroupGetter getter = mock(UserGroupGetter.class); 19 | 20 | when(getter.getGroup()).thenReturn("group"); 21 | when(getter.getUser()).thenReturn("user"); 22 | 23 | 24 | LocalVirtualMachine lvm = mock(LocalVirtualMachine.class); 25 | when(lvm.isManageable()).thenReturn(true); 26 | when(lvm.connectorAddress()).thenReturn("address"); 27 | 28 | jvmAttach.setLvm(lvm); 29 | 30 | when(jvmAttach.getUserGroupGetter()).thenReturn(getter); 31 | when(jvmAttach.getLocalVirtualMachine(0)).thenReturn(lvm); 32 | when(jvmAttach.execCommand(JVMAttach.CommandStrategy.Su)).thenReturn(0); 33 | when(jvmAttach.execCommand(JVMAttach.CommandStrategy.Sudo)).thenReturn(0); 34 | when(jvmAttach.execCommand(JVMAttach.CommandStrategy.SudoWithGroup)).thenReturn(0); 35 | 36 | 37 | jvmAttach.attach(); 38 | String[] args = jvmAttach.getArgumentsBy(JVMAttach.CommandStrategy.Su); 39 | assertEquals(5, args.length); 40 | for (String arg : args) { 41 | assertNotNull(arg); 42 | } 43 | args = jvmAttach.getArgumentsBy(JVMAttach.CommandStrategy.Sudo); 44 | assertEquals(9, args.length); 45 | for (String arg : args) { 46 | assertNotNull(arg); 47 | } 48 | args = jvmAttach.getArgumentsBy(JVMAttach.CommandStrategy.SudoWithGroup); 49 | assertEquals(7, args.length); 50 | for (String arg : args) { 51 | assertNotNull(arg); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /jmx-collector/src/test/java/com/objectstyle/appfirst/jmx/collector/config/ThresholdBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.RangeThresholdDefinition; 4 | import com.objectstyle.appfirst.jmx.collector.command.ThresholdDefinition; 5 | import org.junit.Test; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | public class ThresholdBuilderTest { 10 | @Test 11 | public void testFromString() throws Exception { 12 | ThresholdBuilder builder = new ThresholdBuilder(); 13 | ThresholdDefinition thresholdDefinition = builder.fromString("10"); 14 | assertTrue(thresholdDefinition instanceof RangeThresholdDefinition); 15 | 16 | RangeThresholdDefinition definition = ((RangeThresholdDefinition) thresholdDefinition); 17 | assertEquals(0, definition.getLeftBorder()); 18 | assertEquals(10, definition.getRightBorder()); 19 | assertFalse(definition.isInside()); 20 | 21 | definition = (RangeThresholdDefinition) builder.fromString("10:"); 22 | assertEquals(10, definition.getLeftBorder()); 23 | assertEquals(Long.MAX_VALUE, definition.getRightBorder()); 24 | assertFalse(definition.isInside()); 25 | 26 | definition = (RangeThresholdDefinition) builder.fromString("~:10"); 27 | assertEquals(Long.MIN_VALUE, definition.getLeftBorder()); 28 | assertEquals(10, definition.getRightBorder()); 29 | assertFalse(definition.isInside()); 30 | 31 | definition = (RangeThresholdDefinition) builder.fromString("10:20"); 32 | assertEquals(10, definition.getLeftBorder()); 33 | assertEquals(20, definition.getRightBorder()); 34 | assertFalse(definition.isInside()); 35 | 36 | definition = (RangeThresholdDefinition) builder.fromString("@10:20"); 37 | assertEquals(10, definition.getLeftBorder()); 38 | assertEquals(20, definition.getRightBorder()); 39 | assertTrue(definition.isInside()); 40 | 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/output/AfCollectorOutput.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.output; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.output.af.AfCollector; 4 | import com.objectstyle.appfirst.jmx.collector.result.Result; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import static com.objectstyle.appfirst.jmx.collector.output.af.AfCollector.AFCollectorReturnCode; 9 | import static com.objectstyle.appfirst.jmx.collector.output.af.AfCollector.postAFCollectorMessage; 10 | 11 | public class AfCollectorOutput extends QueuedOutput implements Output { 12 | private static final Logger LOGGER = LoggerFactory.getLogger(AfCollectorOutput.class); 13 | 14 | @Override 15 | protected void doWrite(Result result) { 16 | // This is our declared format 17 | /*String fullString = String.format("jmx_command[%s];status=%d;message=\"%s\"", 18 | result.getName(), result.getStatus().getNagiosIntValue(), result.toString());*/ 19 | 20 | // This is format that proposed Pat 21 | String fullString = String.format("%s:%s:%d", result.getName(), 22 | result.toString(), result.getStatus().getNagiosIntValue()); 23 | 24 | LOGGER.debug("Posting message to the AppFirst collector: {}", fullString); 25 | AFCollectorReturnCode returnCode 26 | = postAFCollectorMessage(AfCollector.AFCollectorMsgSeverity.AFCSeverityPolled, fullString); 27 | LOGGER.debug("Returned code from the AppFirst collector: {}", returnCode); 28 | 29 | while (returnCode == AFCollectorReturnCode.AFCWouldBlock) { 30 | try { 31 | Thread.sleep(1000); 32 | } catch (InterruptedException e) { 33 | // ignoring 34 | } 35 | LOGGER.debug("Trying to post message to the AppFirst collector again: {}", fullString); 36 | returnCode = postAFCollectorMessage(AfCollector.AFCollectorMsgSeverity.AFCSeverityPolled, fullString); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/ThresholdBuilder.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.NoThresholdDefinition; 4 | import com.objectstyle.appfirst.jmx.collector.command.RangeThresholdDefinition; 5 | import com.objectstyle.appfirst.jmx.collector.command.ThresholdDefinition; 6 | 7 | public class ThresholdBuilder { 8 | public ThresholdDefinition fromString(String valueString) throws ThresholdValidationException { 9 | if (valueString == null || valueString.isEmpty()) { 10 | return new NoThresholdDefinition(); 11 | } 12 | 13 | boolean inside = false; 14 | if (valueString.startsWith("@")) { 15 | inside = true; 16 | valueString = valueString.substring(1); 17 | } 18 | 19 | long leftRange; 20 | long rightRange; 21 | String[] rangeStrings = valueString.split(":"); 22 | if (rangeStrings.length == 2 || valueString.endsWith(":")) { 23 | String leftString = rangeStrings[0]; 24 | String rightString = rangeStrings.length == 2 ? rangeStrings[1] : ""; 25 | if (leftString.isEmpty()) { 26 | throw new ThresholdValidationException(); 27 | } 28 | leftRange = leftString.equals("~") ? Long.MIN_VALUE : Long.valueOf(leftString); 29 | rightRange = rightString.equals("~") || rightString.isEmpty() ? Long.MAX_VALUE : Long.valueOf(rightString); 30 | } else if (rangeStrings.length == 1) { 31 | leftRange = 0; 32 | String rightString = rangeStrings[0]; 33 | if (rightString.isEmpty()) { 34 | throw new ThresholdValidationException(); 35 | } 36 | rightRange = rightString.equals("~") ? Long.MAX_VALUE : Long.valueOf(rightString); 37 | } else { 38 | throw new ThresholdValidationException(); 39 | } 40 | 41 | return new RangeThresholdDefinition(leftRange, rightRange, inside); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/management/JMXConnectorPool.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.management; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import javax.management.remote.JMXConnector; 7 | import javax.management.remote.JMXConnectorFactory; 8 | import javax.management.remote.JMXServiceURL; 9 | import java.io.IOException; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | class JMXConnectorPool { 14 | private static final Logger LOGGER = LoggerFactory.getLogger(JMXConnectorPool.class); 15 | 16 | private Map connectors = new HashMap(); 17 | 18 | public JMXConnector get(JMXServiceURL url) throws IOException { 19 | JMXConnector connector = getConnector(url); 20 | if (connector == null) { 21 | LOGGER.debug("Creating new JMX connector for URL {}", url); 22 | connector = JMXConnectorFactory.connect(url); 23 | connectors.put(url, connector); 24 | } 25 | return connector; 26 | } 27 | 28 | public void remove(JMXServiceURL url) { 29 | LOGGER.debug("Removing JMX connector from the pool for URL {}", url); 30 | JMXConnector connector = connectors.remove(url); 31 | if (connector != null) { 32 | try { 33 | connector.close(); 34 | } catch (IOException e) { 35 | LOGGER.warn("Error while removing JMX connector from the pool", e); 36 | } 37 | } 38 | } 39 | 40 | private JMXConnector getConnector(JMXServiceURL url) { 41 | JMXConnector connector = connectors.get(url); 42 | if (connector == null) { 43 | LOGGER.debug("JMX connector for URL {} was found", url); 44 | return null; 45 | } 46 | try { 47 | LOGGER.debug("Found JMX connector for URL {}", url); 48 | String connectionId = connector.getConnectionId(); 49 | LOGGER.debug("Connection for URL {} with id {} is active.", url, connectionId); 50 | return connector; 51 | } catch (IOException e) { 52 | LOGGER.warn("Cannot get connection id. the connection was closed probably.", e); 53 | return null; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/output/QueuedOutput.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.output; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.Result; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import java.util.concurrent.BlockingQueue; 8 | import java.util.concurrent.ExecutorService; 9 | import java.util.concurrent.Executors; 10 | import java.util.concurrent.LinkedBlockingQueue; 11 | 12 | public abstract class QueuedOutput implements Output { 13 | private static final Logger LOGGER = LoggerFactory.getLogger(QueuedOutput.class); 14 | 15 | private final BlockingQueue results; 16 | 17 | private class ResultsWriteRunnable implements Runnable { 18 | @Override 19 | public void run() { 20 | while (true) { 21 | try { 22 | LOGGER.debug("Waiting for result to write"); 23 | Result result = results.take(); 24 | LOGGER.debug("Took result from the queue: {}", result); 25 | doWrite(result); 26 | } catch (InterruptedException e) { 27 | LOGGER.warn("Result send has been interrupted", e); 28 | } catch (Throwable th) { 29 | LOGGER.error("Error while writing result", th); 30 | } 31 | } 32 | } 33 | } 34 | 35 | protected QueuedOutput() { 36 | this.results = new LinkedBlockingQueue(); 37 | ExecutorService writeExecutor = Executors.newSingleThreadExecutor(); 38 | writeExecutor.submit(new ResultsWriteRunnable()); 39 | } 40 | 41 | @Override 42 | public final void write(Result result) { 43 | try { 44 | LOGGER.debug("Putting result to the queue: {}", result); 45 | results.put(result); 46 | } catch (InterruptedException e) { 47 | LOGGER.warn("Interrupted while putting result to the queue"); 48 | } 49 | } 50 | 51 | protected abstract void doWrite(Result result); 52 | } 53 | -------------------------------------------------------------------------------- /test/test_check_mongodb.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys 7 | import os 8 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 9 | sys.path.append(os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from test_plugin import TestPlugin 13 | from check_mongodb import MongoDBChecker 14 | 15 | class TestMongoDBChecker(TestPlugin): 16 | def setUp(self): 17 | self.checker = MongoDBChecker() 18 | try: 19 | os.mkdir("./status/") 20 | except OSError: 21 | pass 22 | print 'check_mongodb' 23 | 24 | def test_get_connections(self): 25 | self.assert_status("-t CONNECTIONS -z mongodb_test -d ./status/") 26 | 27 | def test_get_memory_used(self): 28 | self.assert_status("-t MEMORY_USED -z mongodb_test -d ./status/") 29 | 30 | def test_get_insert_rate(self): 31 | self.assert_status("-t INSERT -z mongodb_test -d ./status/") 32 | 33 | def test_get_update_rate(self): 34 | self.assert_status("-t UPDATE -z mongodb_test -d ./status/") 35 | 36 | def test_get_command_rate(self): 37 | self.assert_status("-t COMMAND -z mongodb_test -d ./status/") 38 | 39 | def test_get_query_rate(self): 40 | self.assert_status("-t QUERY -z mongodb_test -d ./status/") 41 | 42 | def test_get_delete_rate(self): 43 | self.assert_status("-t DELETE -z mongodb_test -d ./status/") 44 | 45 | def test_get_locked_ratio(self): 46 | self.assert_status("-t LOCKED_PERCENTAGE -z mongodb_test -d ./status/") 47 | 48 | def test_get_miss_ratio(self): 49 | self.assert_status("-t MISS_PERCENTAGE -z mongodb_test -d ./status/") 50 | 51 | def test_get_resets(self): 52 | self.assert_status("-t RESETS -z mongodb_test -d ./status/") 53 | 54 | def test_get_hits(self): 55 | self.assert_status("-t HITS -z mongodb_test -d ./status/") 56 | 57 | def test_get_misses(self): 58 | self.assert_status("-t MISSES -z mongodb_test -d ./status/") 59 | 60 | def test_get_accesses(self): 61 | self.assert_status("-t ACCESSES -z mongodb_test -d ./status/") 62 | 63 | if __name__ == "__main__": 64 | unittest.main() -------------------------------------------------------------------------------- /apache_log/plugins/urls_counter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import sys 3 | import logging 4 | import os 5 | import re 6 | 7 | LOGGER = logging.getLogger(__name__) 8 | 9 | class UrlsCounter(): 10 | 11 | def __init__(self, outputFilePath, tags = None, statsdPrefix = 'apache_url_counter'): 12 | logging.debug('creating UrlsCounter to file ' + outputFilePath) 13 | self.outputFilePath = outputFilePath 14 | self.check_output_file() 15 | self.statsdPrefix = statsdPrefix 16 | 17 | self.tagsEnabled = tags 18 | 19 | def check_output_file(self): 20 | if not os.path.isfile(self.outputFilePath): 21 | f = file(self.outputFilePath, 'w') 22 | f.close() 23 | 24 | def stringFormatter(self, item, url): 25 | tag = '' 26 | if self.tagsEnabled: 27 | if len(self.tagsEnabled) > 1: 28 | tag = self.tagsEnabled 29 | else: 30 | tag = ':AF_UrlRequestCount:' 31 | return tag + ' [' + item['date'] + '] ' + str(item['count']) + ' - ' + url + os.linesep 32 | 33 | def append_to_file(self, urls): 34 | logging.debug('uppending to file ' + self.outputFilePath) 35 | if len(urls) > 0: 36 | try: 37 | f = file(self.outputFilePath, 'a') 38 | for url in urls: 39 | s = self.stringFormatter(urls[url], url); 40 | #LOGGER.info(s) 41 | f.write(s) 42 | 43 | f.close() 44 | except Exception as e: 45 | LOGGER.critical('Serious Error occured: %s', e) 46 | 47 | 48 | 49 | def update(self, urls): 50 | urlsCount = {} 51 | logging.debug('updating to file ' + self.outputFilePath) 52 | if len(urls) > 0: 53 | 54 | for url in urls: 55 | if not url['url'] in urlsCount.keys(): 56 | urlsCount[url['url']] = {'date': url['date'], 'count': 1} 57 | else: 58 | urlsCount[url['url']]['date'] = url['date'] 59 | urlsCount[url['url']]['count'] += 1 60 | 61 | 62 | self.append_to_file(urlsCount) 63 | return urlsCount 64 | 65 | -------------------------------------------------------------------------------- /test/test_check_postgresql.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys 7 | import os 8 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 9 | sys.path.append(os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from test_plugin import TestPlugin 13 | from check_postgresql import PostgresChecker 14 | 15 | class TestPostgresChecker(TestPlugin): 16 | def setUp(self): 17 | self.checker = PostgresChecker() 18 | try: 19 | os.mkdir("./status/") 20 | except OSError: 21 | pass 22 | print 'check_postgresql' 23 | 24 | def test_get_connections_active(self): 25 | self.assert_status("-t CONNECTIONS_ACTIVE -z postgres_test -d ./status/") 26 | 27 | def test_get_connections_waiting(self): 28 | self.assert_status("-t CONNECTIONS_WAITING -z postgres_test -d ./status/") 29 | 30 | def test_get_conenctions_idle(self): 31 | self.assert_status("-t CONNECTIONS_IDLE -z postgres_test -d ./status/") 32 | 33 | def test_get_database_size(self): 34 | self.assert_status("-t DATABASE_SIZE -z postgres_test -d ./status/") 35 | 36 | def test_get_locks_access(self): 37 | self.assert_status("-t LOCKS_ACCESS -z postgres_test -d ./status/") 38 | 39 | def test_get_locks_row(self): 40 | self.assert_status("-t LOCKS_ROW -z postgres_test -d ./status/") 41 | 42 | def test_get_locks_share(self): 43 | self.assert_status("-t LOCKS_SHARE -z postgres_test -d ./status/") 44 | 45 | def test_get_locks_exclusive(self): 46 | self.assert_status("-t LOCKS_EXCLUSIVE -z postgres_test -d ./status/") 47 | 48 | def test_get_tuple_read(self): 49 | self.assert_status("-t TUPLES_READ -z postgres_test -d ./status/") 50 | 51 | def test_get_tuple_inserted(self): 52 | self.assert_status("-t TUPLES_INSERTED -z postgres_test -d ./status/") 53 | 54 | def test_get_tuple_updated(self): 55 | self.assert_status("-t TUPLES_UPDATED -z postgres_test -d ./status/") 56 | 57 | def test_get_tuple_deleted(self): 58 | self.assert_status("-t TUPLES_DELETED -z postgres_test -d ./status/") 59 | 60 | if __name__ == "__main__": 61 | unittest.main() -------------------------------------------------------------------------------- /af.oracle.metrics/src/main/java/com/os/appfirst/OracleSqlQuerys.java: -------------------------------------------------------------------------------- 1 | package com.os.appfirst; 2 | 3 | import java.sql.DriverManager; 4 | import java.sql.Connection; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.sql.Statement; 8 | 9 | import org.apache.log4j.Logger; 10 | 11 | public class OracleSqlQuerys { 12 | 13 | static final Logger logger = Logger.getLogger(App.class); 14 | private Connection connection = null; 15 | 16 | public Boolean connect(String oracleDbUrl, String user, String pass) { 17 | 18 | logger.debug("Oracle JDBC Connecting"); 19 | 20 | try { 21 | 22 | Class.forName("oracle.jdbc.driver.OracleDriver"); 23 | 24 | } catch (ClassNotFoundException e) { 25 | 26 | logger.fatal("Oracle JDBC Driver not found " + e.getMessage()); 27 | return false; 28 | 29 | } 30 | 31 | logger.debug("Oracle JDBC Driver Registered!"); 32 | 33 | try { 34 | 35 | //"jdbc:oracle:thin:@192.168.1.66:1521:oracle" 36 | connection = DriverManager.getConnection(oracleDbUrl, user, pass); 37 | 38 | } catch (SQLException e) { 39 | 40 | logger.debug("Connection Failed! Check output console " + e.getMessage()); 41 | return false; 42 | 43 | } 44 | 45 | if (connection != null) { 46 | logger.debug("connection established to Oracle DB"); 47 | } else { 48 | logger.fatal("Failed to make connection!"); 49 | return false; 50 | } 51 | 52 | return true; 53 | } 54 | 55 | public ResultSet makeQuery(String sql) { 56 | Statement stmt = null; 57 | try { 58 | if (this.connection != null) { 59 | stmt = this.connection.createStatement(); 60 | 61 | if (stmt != null) { 62 | if (sql != null) { 63 | ResultSet rs = stmt.executeQuery(sql); 64 | return rs; 65 | } else { 66 | logger.fatal("sql query not set"); 67 | } 68 | 69 | } else { 70 | logger.fatal("createStatement error"); 71 | } 72 | } 73 | } catch (SQLException e) { 74 | try { 75 | stmt.close(); 76 | this.connection.close(); 77 | logger.fatal("SQL error: " + e.getMessage()); 78 | } catch (SQLException err) { 79 | logger.fatal("Exception in closing DB resources " + err.getMessage()); 80 | } 81 | } 82 | return null; 83 | 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/command/Command.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.command; 2 | 3 | import com.google.common.base.Objects; 4 | 5 | public class Command { 6 | private final String name; 7 | 8 | private final VirtualMachineDefinition virtualMachineDefinition; 9 | 10 | private final ValueDefinition valueDefinition; 11 | 12 | private final ThresholdDefinition warningThresholdDefinition; 13 | 14 | private final ThresholdDefinition criticalThresholdDefinition; 15 | 16 | public Command(String name, VirtualMachineDefinition virtualMachineDefinition, ValueDefinition valueDefinition, 17 | ThresholdDefinition warningThresholdDefinition, ThresholdDefinition criticalThresholdDefinition) { 18 | this.name = name; 19 | this.virtualMachineDefinition = virtualMachineDefinition; 20 | this.valueDefinition = valueDefinition; 21 | this.warningThresholdDefinition = warningThresholdDefinition; 22 | this.criticalThresholdDefinition = criticalThresholdDefinition; 23 | } 24 | 25 | public Command(String name, VirtualMachineDefinition virtualMachineDefinition, ValueDefinition valueDefinition) { 26 | this(name, virtualMachineDefinition, valueDefinition, new NoThresholdDefinition(), new NoThresholdDefinition()); 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public VirtualMachineDefinition getVirtualMachineDefinition() { 34 | return virtualMachineDefinition; 35 | } 36 | 37 | public ValueDefinition getValueDefinition() { 38 | return valueDefinition; 39 | } 40 | 41 | public ThresholdDefinition getWarningThresholdDefinition() { 42 | return warningThresholdDefinition; 43 | } 44 | 45 | public ThresholdDefinition getCriticalThresholdDefinition() { 46 | return criticalThresholdDefinition; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return Objects.toStringHelper(this) 52 | .add("name", name) 53 | .add("virtualMachineDefinition", virtualMachineDefinition) 54 | .add("valueDefinition", valueDefinition) 55 | .toString(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /apache_log/plugins/app_thread.py: -------------------------------------------------------------------------------- 1 | 2 | import logging 3 | import socket 4 | import select 5 | from threading import Thread 6 | 7 | 8 | LOGGER = logging.getLogger(__name__) 9 | 10 | 11 | 12 | class ProcessThread(Thread): 13 | 14 | 15 | def __init__(self, host = '127.0.0.1', port = 30330): 16 | super(ProcessThread, self).__init__() 17 | self.running = True 18 | self._socket = None 19 | self._data = None 20 | self.host = host 21 | self.port = port 22 | 23 | 24 | def setData(self, data): 25 | LOGGER.debug(' set data {i}'.format(i=data)) 26 | self._data = data 27 | 28 | def stop(self): 29 | LOGGER.debug ('ProcessThread stop') 30 | self.running = False 31 | self._socket.shutdown(socket.SHUT_WR) 32 | self._socket.close() 33 | 34 | def checkData(self): 35 | 36 | try: 37 | LOGGER.debug ('checking data...') 38 | client, addr = self._socket.accept() 39 | ready = select.select([client,],[], [],2) 40 | if ready[0]: 41 | LOGGER.debug ('client.recv...') 42 | data = client.recv(512) 43 | LOGGER.debug ('client.send...') 44 | if self._data is not None: 45 | client.send(str(self._data)) 46 | else: 47 | client.send(str(-1)) 48 | LOGGER.debug ('data was received:' + data) 49 | self._data = None; 50 | return True 51 | except KeyboardInterrupt: 52 | self._socket.close() 53 | LOGGER.debug('stop.') 54 | except socket.error, msg: 55 | self._socket.close() 56 | LOGGER.debug ('socket error: %s' % msg) 57 | 58 | return False 59 | 60 | def run(self): 61 | 62 | if self._socket is None: 63 | self._socket = socket.socket() 64 | 65 | self._socket.bind((self.host, self.port)) 66 | LOGGER.debug('Listening on {h} {p}...'.format(p = self.port, h = self.host)) 67 | self._socket.listen(1) 68 | 69 | while self.running: 70 | try: 71 | LOGGER.debug('server is running ...') 72 | self.checkData() 73 | # time.sleep(1) 74 | except KeyboardInterrupt: 75 | self.stop() 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /test/test_plugin.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 20, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import sys, os 7 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 8 | sys.path.append( 9 | os.path.join(_rootpath, "..")) 10 | 11 | import unittest 12 | from StringIO import StringIO 13 | import nagios 14 | import time 15 | 16 | class TestPlugin(unittest.TestCase): 17 | def assert_status(self, argument, status_code=nagios.Status.OK): 18 | # get output and exit_code 19 | exit_code = 0 20 | start = time.time() 21 | try: 22 | # change the standard output to StringIO() 23 | sio = StringIO() 24 | original_stdout, sys.stdout = sys.stdout, sio 25 | args = argument.split() 26 | self.checker.run(args) 27 | except SystemExit as se: 28 | exit_code = se.code 29 | finally: 30 | # change the standard output back 31 | sys.stdout = original_stdout 32 | output = sio.getvalue() 33 | sio.close() 34 | duration = time.time() - start 35 | # print and assert 36 | print "\targv: %s" % argument 37 | print "\tstdout: %s" % output.rstrip() 38 | print "\tran in: %s secs" % duration 39 | self.assertEqual(exit_code, status_code, 40 | "return %s, expecting %s" %( 41 | nagios.Status.to_status(exit_code), 42 | nagios.Status.to_status(status_code))) 43 | 44 | def print_status(self, argument): 45 | # get output and exit_code 46 | exit_code = 0 47 | start = time.time() 48 | try: 49 | # change the standard output to StringIO() 50 | sio = StringIO() 51 | original_stdout, sys.stdout = sys.stdout, sio 52 | args = argument.split() 53 | self.checker.run(args) 54 | except SystemExit as se: 55 | exit_code = se.code 56 | finally: 57 | # change the standard output back 58 | sys.stdout = original_stdout 59 | output = sio.getvalue() 60 | sio.close() 61 | duration = time.time() - start 62 | # print and assert 63 | print "\targv: %s" % argument 64 | print "\tstdout: %s" % output.rstrip() 65 | print "\tran in: %s secs" % duration 66 | print "\tstatus: %s" % exit_code -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/management/ManagementConnectionFactory.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.management; 2 | 3 | import com.google.common.collect.HashMultimap; 4 | import com.google.common.collect.SetMultimap; 5 | import com.objectstyle.appfirst.jmx.collector.command.VirtualMachineDefinition; 6 | import com.objectstyle.appfirst.jmx.collector.resolve.*; 7 | 8 | import javax.management.MBeanServerConnection; 9 | import javax.management.remote.JMXServiceURL; 10 | import java.io.IOException; 11 | 12 | public class ManagementConnectionFactory implements VirtualMachineIdentifierInvalidationListener { 13 | private JMXConnectorPool connectorPool = new JMXConnectorPool(); 14 | 15 | private VirtualMachineResolver resolver = new CachingLocalVirtualMachineResolver(); 16 | 17 | private SetMultimap availableIdentifiers = HashMultimap.create(); 18 | 19 | public synchronized MBeanServerConnection getConnection(VirtualMachineDefinition definition) 20 | throws VirtualMachineResolverException, IOException { 21 | VirtualMachineIdentifier identifier = resolveIdentifier(definition); 22 | return getConnection(identifier); 23 | } 24 | 25 | private VirtualMachineIdentifier resolveIdentifier(VirtualMachineDefinition definition) 26 | throws VirtualMachineResolverException { 27 | VirtualMachineIdentifier identifier = resolver.resolve(definition); 28 | availableIdentifiers.put(identifier.getUrl(), identifier); 29 | identifier.addInvalidationListener(this); 30 | return identifier; 31 | } 32 | 33 | private MBeanServerConnection getConnection(VirtualMachineIdentifier identifier) throws IOException { 34 | return connectorPool.get(identifier.getUrl()).getMBeanServerConnection(); 35 | } 36 | 37 | @Override 38 | public synchronized void identifierInvalidated(VirtualMachineIdentifier identifier) { 39 | availableIdentifiers.remove(identifier.getUrl(), identifier); 40 | if (availableIdentifiers.get(identifier.getUrl()).isEmpty()) { 41 | connectorPool.remove(identifier.getUrl()); 42 | } 43 | } 44 | 45 | public void closeConnection(VirtualMachineDefinition definition) { 46 | try { 47 | resolveIdentifier(definition).invalidate(); 48 | } catch (VirtualMachineResolverException e) { 49 | // if virtual machine could not been found then connection for it might had already been closed 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/Connect.java: -------------------------------------------------------------------------------- 1 | import com.objectstyle.appfirst.jmx.collector.utils.JVMAttach; 2 | import sun.tools.jconsole.LocalVirtualMachine; 3 | 4 | import javax.management.*; 5 | import javax.management.remote.JMXConnector; 6 | import javax.management.remote.JMXConnectorFactory; 7 | import javax.management.remote.JMXServiceURL; 8 | import java.io.IOException; 9 | import java.util.Map; 10 | import java.util.Set; 11 | 12 | /** 13 | * Created with IntelliJ IDEA. 14 | * User: admin 15 | * Date: 9/9/12 16 | * Time: 4:58 PM 17 | * To change this template use File | Settings | File Templates. 18 | */ 19 | public class Connect { 20 | 21 | public static void main(String[] args) throws IOException { 22 | list(); 23 | } 24 | 25 | private static void list() throws IOException { 26 | Map vms = LocalVirtualMachine.getAllVirtualMachines(); 27 | for (LocalVirtualMachine lvm : vms.values()) { 28 | System.out.println(String.format("lvm.id = %d; lvm.displayName = %s, lvm.manageable = %b,lvm.attachable = %b", lvm.vmid(), lvm.displayName(), lvm.isManageable(), lvm.isAttachable())); 29 | JVMAttach attach = new JVMAttach(); 30 | attach.setLvm(lvm); 31 | attach.attach(); 32 | lvm = LocalVirtualMachine.getLocalVirtualMachine(lvm.vmid()); 33 | getListBeans(lvm); 34 | System.out.println(String.format("lvm.id = %d; lvm.displayName = %s, lvm.manageable = %b,lvm.attachable = %b", lvm.vmid(), lvm.displayName(), lvm.isManageable(), lvm.isAttachable())); 35 | } 36 | } 37 | 38 | 39 | private static void getListBeans(LocalVirtualMachine lvm){ 40 | try { 41 | JMXServiceURL jmxUrl = new JMXServiceURL(lvm.connectorAddress()); 42 | JMXConnector jmxc = JMXConnectorFactory.connect(jmxUrl); 43 | 44 | MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); 45 | 46 | Set beans = mbsc.queryMBeans(null, null); 47 | 48 | String template = "%s.%s] -P -Dsolr -O %s -A %s"; 49 | for (ObjectInstance instance : beans) { 50 | 51 | MBeanInfo beanInfo = mbsc.getMBeanInfo(instance.getObjectName()); 52 | 53 | MBeanAttributeInfo[] attributes = beanInfo.getAttributes(); 54 | for (MBeanAttributeInfo attributeInfo : attributes) { 55 | if (!attributeInfo.isWritable() && 56 | !attributeInfo.getType().equals("java.util.Map")) 57 | System.out.println(String.format(template, instance.getObjectName().getKeyProperty("type"), attributeInfo.getName(), instance.getObjectName(), attributeInfo.getName())); 58 | } 59 | } 60 | } catch (Throwable e) { 61 | e.printStackTrace(); 62 | } 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /af.oracle.metrics/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /check_passenger.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | ''' 3 | Created on Jun 7, 2012 4 | 5 | @author: Yangming 6 | @copyright: appfirst inc. 7 | ''' 8 | import commands 9 | import statsd 10 | import nagios 11 | from nagios import CommandBasedPlugin as plugin 12 | 13 | class PassengerChecker(nagios.BatchStatusPlugin): 14 | def __init__(self, *args, **kwargs): 15 | super(PassengerChecker, self).__init__(*args, **kwargs) 16 | self.parser.add_argument("-f", "--filename", required=False, type=str, default="pd@passenger-status") 17 | self.parser.add_argument("-p", "--pid", required=False, type=str) 18 | self.parser.add_argument("-z", "--appname", required=False, type=str, default='passenger') 19 | 20 | def _get_batch_status(self, request): 21 | # TODO: make path optional 22 | cmd = nagios.rootify("/usr/bin/passenger-status") 23 | return commands.getoutput(cmd) 24 | 25 | def _parse_output(self, request, output): 26 | for l in output.split('\n'): 27 | pair = l.split('=') 28 | if len(pair) == 2: 29 | k = pair[0].strip() 30 | v = pair[1].strip() 31 | value = nagios.to_num(v) 32 | if value is None: 33 | raise nagios.OutputFormatError(request, output) 34 | yield k, value 35 | 36 | def _validate_output(self, request, output): 37 | if "command not found" in output: 38 | raise nagios.ServiceInaccessibleError(request, output) 39 | elif "ERROR: Phusion Passenger doesn't seem to be running." in output: 40 | raise nagios.ServiceInaccessibleError(request, output) 41 | elif "ERROR: You are not authorized" in output: 42 | raise nagios.AuthenticationFailedError(request, output) 43 | elif "ERROR" in output: 44 | raise nagios.StatusUnknownError(request, output) 45 | return True 46 | 47 | @plugin.command("RUNNING_PROCESSES") 48 | @statsd.gauge 49 | def get_procs(self, request): 50 | value = self.get_status_value("count", request) 51 | return self.get_result(request, value, '%s running processes' % value, 'procs') 52 | 53 | @plugin.command("MAX_PROCESSES") 54 | @statsd.gauge 55 | def get_max_procs(self, request): 56 | value = self.get_status_value("max", request) 57 | return self.get_result(request, value, '%s max processes' % value, 'max') 58 | 59 | @plugin.command("ACTIVE_PROCESSES") 60 | @statsd.gauge 61 | def get_active_procs(self, request): 62 | value = self.get_status_value("active", request) 63 | return self.get_result(request, value, '%s active processes' % value, 'active') 64 | 65 | if __name__ == "__main__": 66 | import sys 67 | PassengerChecker().run(sys.argv[1:]) -------------------------------------------------------------------------------- /statsd.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Jun 14, 2012 3 | 4 | @author: Yangming 5 | ''' 6 | import os, sys 7 | _rootpath = os.path.dirname(os.path.realpath(__file__)) 8 | sys.path.append(os.path.join(_rootpath, "statsd")) 9 | try: 10 | from afstatsd import Statsd, AFTransport 11 | Statsd.set_transport(AFTransport()) 12 | except: 13 | print "Statsd Library is not available, check PYTHON_PATH" 14 | Statsd = None 15 | 16 | TIMER_BUCKET_PATTERN = "sys.app.timer.%(appname)s.%(name)s" 17 | COUNTER_BUCKET_PATTERN = "sys.app.counter.%(appname)s.%(name)s" 18 | GAUGE_BUCKET_PATTERN = "sys.app.gauge.%(appname)s.%(name)s" 19 | 20 | def set_timer_bucket_pattern(pattern): 21 | global TIMER_BUCKET_PATTERN 22 | TIMER_BUCKET_PATTERN = pattern 23 | 24 | def set_counter_bucket_pattern(pattern): 25 | global COUNTER_BUCKET_PATTERN 26 | COUNTER_BUCKET_PATTERN = pattern 27 | 28 | def set_gauge_bucket_pattern(pattern): 29 | global GAUGE_BUCKET_PATTERN 30 | GAUGE_BUCKET_PATTERN = pattern 31 | 32 | def timer(method): 33 | def send_statsd(*args, **kwargs): 34 | result = method(*args, **kwargs) 35 | bucket = TIMER_BUCKET_PATTERN % result 36 | # TODO: deal with more than one performance data 37 | if len(result.perf_data_list): 38 | value = result.perf_data_list[0]['value'] 39 | Statsd.timing(bucket, value) 40 | return result 41 | if Statsd: 42 | return send_statsd 43 | else: 44 | return method 45 | 46 | def counter(method): 47 | def send_statsd(*args, **kwargs): 48 | result = method(*args, **kwargs) 49 | bucket = COUNTER_BUCKET_PATTERN % result 50 | # TODO: deal with more than one performance data 51 | if len(result.perf_data_list): 52 | value = result.perf_data_list[0]['value'] 53 | Statsd.update_stats(bucket) 54 | return result 55 | if Statsd: 56 | return send_statsd 57 | else: 58 | return method 59 | 60 | def gauge(method): 61 | def send_statsd(*args, **kwargs): 62 | result = method(*args, **kwargs) 63 | bucket = GAUGE_BUCKET_PATTERN % result 64 | # TODO: deal with more than one performance data 65 | if len(result.perf_data_list): 66 | value = result.perf_data_list[0]['value'] 67 | Statsd.gauge(bucket, value) 68 | return result 69 | if Statsd: 70 | return send_statsd 71 | else: 72 | return method 73 | 74 | if __name__ == "__main__": 75 | import nagios 76 | Statsd._transport.verbosity = True 77 | @timer 78 | @gauge 79 | @counter 80 | def test_wrapper(): 81 | result = nagios.Result("STATSWRAPPER", 0, appname="nagios") 82 | result.add_performance_data("total", 1) 83 | return result 84 | test_wrapper() 85 | -------------------------------------------------------------------------------- /jmx-collector/src/test/java/com/objectstyle/appfirst/jmx/collector/config/SimpleCommandLinesSourceTest.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import com.google.common.io.InputSupplier; 4 | import org.jmock.Expectations; 5 | import org.jmock.Mockery; 6 | import org.jmock.api.Invocation; 7 | import org.jmock.auto.Mock; 8 | import org.jmock.integration.junit4.JMock; 9 | import org.jmock.integration.junit4.JUnit4Mockery; 10 | import org.jmock.lib.action.CustomAction; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | 14 | import java.io.IOException; 15 | import java.nio.CharBuffer; 16 | import java.util.List; 17 | 18 | import static junit.framework.Assert.assertEquals; 19 | import static junit.framework.Assert.assertTrue; 20 | import static junit.framework.Assert.assertFalse; 21 | 22 | @RunWith(JMock.class) 23 | public class SimpleCommandLinesSourceTest { 24 | private static final String TEST_CONFIG_DATA = "command[check_load]=/usr/share/appfirst/plugins/libexec/check_load -w 15,10,5 -c 30,25,20\n" 25 | + "jmx_command[Application.Runtime.Name] -P com.objectstyle.Application -O java.lang:type=Runtime -A name\u2028\u2028\n" 26 | + "jmx_command[Application.Memory.HeapMemoryUsage.Used] -P com.objectstyle.Application -O java.lang:type=Memory -A heapMemoryUsage -K used"; 27 | 28 | private Mockery context = new JUnit4Mockery(); 29 | 30 | @Mock 31 | private Readable readableMock; 32 | 33 | private class ReadableMockInputSupplier implements InputSupplier { 34 | @Override 35 | public Readable getInput() throws IOException { 36 | return readableMock; 37 | } 38 | } 39 | 40 | @Test 41 | public void testReadCommandStrings() throws Exception { 42 | SimpleCommandLinesSource source = new SimpleCommandLinesSource(new ReadableMockInputSupplier()); 43 | 44 | context.checking(new Expectations() { 45 | { 46 | oneOf(readableMock).read(with(aNonNull(CharBuffer.class))); 47 | will(new CustomAction("provide with configuration") { 48 | @Override 49 | public Object invoke(Invocation invocation) throws Throwable { 50 | CharBuffer buffer = (CharBuffer) invocation.getParameter(0); 51 | buffer.append(TEST_CONFIG_DATA); 52 | return TEST_CONFIG_DATA.length(); 53 | } 54 | }); 55 | 56 | allowing(readableMock).read(with(aNonNull(CharBuffer.class))); 57 | will(returnValue(-1)); 58 | } 59 | }); 60 | 61 | List commandStrings = source.readLines(); 62 | assertEquals(2, commandStrings.size()); 63 | for (String string : commandStrings) { 64 | assertTrue(string.startsWith("jmx_command")); 65 | assertFalse(Character.isWhitespace(string.toCharArray()[string.length()-1])); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /check_ipmi_sensors.ps1: -------------------------------------------------------------------------------- 1 | ## Check IMPI Sensors for use with AppFirst Polled Data under windows 2 | ## clark@appfirst.com 3 | ## requires that ipmiutil be installed. 4 | ## http://ipmiutil.sourceforge.net/FILES/ipmiutil-2.9.0.msi 5 | ## Polled Data Config Line: 6 | ## command[sensor_fan]=c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe "c:\Users\Administrator\ipmi\check_ipmi_sensors -t fan" 7 | ## 8 | ## Note that on some systems, you may need to change the bios to enable "Plug & Play BMC Detection" or windows will not load 9 | ## the IPMI driver 10 | ## 11 | Param ( 12 | [parameter(Mandatory=$false)] 13 | [alias("t")] 14 | [string]$sensor_type = "all", 15 | [parameter(Mandatory=$false)] 16 | [alias("c")] 17 | [string]$ipmi_command = "ipmiutil" 18 | ) 19 | 20 | $env:path = $env:path + ";c:\program files (x86)\sourceforge\ipmiutil\" 21 | 22 | if ($sensor_type -eq "all"){ 23 | $ipmiutil_args = "sensor -c" 24 | } 25 | else{ 26 | $ipmiutil_args = "sensor -c -g "+$sensor_type 27 | } 28 | 29 | $ipmi_command += " "+$ipmiutil_args 30 | 31 | [Array]$ipmiutil_output = iex $ipmi_command 32 | 33 | #$ipmiutil_output 34 | 35 | if ($ipmiutil_output.length -lt 3){ 36 | exit(3) 37 | } 38 | 39 | 40 | if ($ipmiutil_output[2].Contains("Unrecognized")){ 41 | exit(3) 42 | } 43 | 44 | if ($ipmiutil_output[2].Contains("Unrecognized")){ 45 | } 46 | 47 | 48 | $status=0 49 | 50 | for ($i=4; $i -lt $ipmiutil_output.length-1; $i++){ 51 | $line = $ipmiutil_output[$i] 52 | if ($line.contains("invalid")){ 53 | continue 54 | } 55 | 56 | [Array]$cols = $line.split("|") 57 | if ($cols[5].contains("warn") -and $status -lt 1){ 58 | $status = 1 59 | } 60 | if ($cols[5].contains("crit") -and $status -lt 1){ 61 | $status = 2 62 | } 63 | } 64 | switch ($status){ 65 | 0 {$ocw = "OK"} 66 | 1 {$ocw = "WARNING"} 67 | 2 {$ocw = "CRITICAL"} 68 | } 69 | 70 | $output = $sensor_type + " " + $ocw +" | " 71 | for ($i=4; $i -lt $ipmiutil_output.length-1; $i++){ 72 | $line = $ipmiutil_output[$i] 73 | if ($line.contains("invalid")){ 74 | continue 75 | } 76 | [Array]$cols = $line.split("|") 77 | if ($cols[6] -ne ""){ 78 | $sensor_name = $cols[4] 79 | $sensor_name = $sensor_name.trim() 80 | $sensor_name = $sensor_name -Replace " ", "_" 81 | $sensor_value = $cols[6] 82 | $sensor_value = $sensor_value.trim() 83 | $output += $sensor_name + "=" + $sensor_value + ";" 84 | } 85 | } 86 | 87 | 88 | ## now scrub the output 89 | #replace multiple spaces with single 90 | $output = $output -Replace "\s{2,}", "" 91 | # remove units (to be nagios compliant) 92 | $output = $output -Replace " RPM;", ";" 93 | $output = $output -Replace " V;", ";" 94 | $output = $output -Replace " C;", ";" 95 | $output = $output -Replace " %;", ";" 96 | # remove trailing ';' if present 97 | $output = $output -Replace ";$", "" 98 | # if no values were report, just status, we can deal with that. Just leave off the '|' 99 | $output = $output -Replace "\| $", "" 100 | 101 | $output 102 | exit ($status) -------------------------------------------------------------------------------- /apache_log/plugins/apache_url_stat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import sys 3 | import logging 4 | import os 5 | import re 6 | 7 | LOGGER = logging.getLogger(__name__) 8 | 9 | 10 | class ApacheLogsParser(): 11 | 12 | def __init__(self, apacheLogFilePath = None): 13 | logging.info('creating ApacheLogsParser') 14 | self.lastModifyed = 0 15 | self.lastLine = 0 16 | self.created = None 17 | 18 | if apacheLogFilePath is None: 19 | LOGGER.critical('apache logs file not defined') 20 | 21 | self.apacheLogFilePath = apacheLogFilePath 22 | 23 | def load_log_file(self, filename, line = 0): 24 | data = None 25 | f = None 26 | if os.path.isfile(filename): 27 | try: 28 | LOGGER.debug('reading log file ' + filename) 29 | lm = os.path.getmtime(filename) 30 | 31 | LOGGER.debug('checking last modification of a file ' + filename + ' ' + str(lm)) 32 | if lm > self.lastModifyed: 33 | f = file(filename, 'r') 34 | lines = f.readlines() 35 | currentLinesNumber = len(lines) 36 | 37 | if (currentLinesNumber < self.lastLine): 38 | LOGGER.debug('reseting last line number from ' + str(self.lastLine) + ' to 0') 39 | self.lastLine = 0 40 | 41 | LOGGER.debug('total records in apache log file ' + str(currentLinesNumber)) 42 | LOGGER.debug('last parsed record(line) ' + str(self.lastLine)) 43 | if currentLinesNumber > self.lastLine: 44 | data = lines[self.lastLine : currentLinesNumber] 45 | self.lastLine = len(lines) 46 | self.lastModifyed = lm 47 | else: 48 | data = [] 49 | finally: 50 | if f is not None: 51 | f.close() 52 | 53 | return data 54 | 55 | def parse(self): 56 | urls = [] 57 | logsList = [] 58 | try: 59 | for logFile in self.apacheLogFilePath: 60 | log = self.load_log_file(logFile) 61 | if log is not None: 62 | logsList.extend( log ) 63 | 64 | if logsList is not None: 65 | for line in logsList: 66 | url = map(';'.join, re.findall(r'([(\d\.)]+) - - (.*?) "(.*?)"', line)) 67 | 68 | if len(url) > 0: 69 | a = url[0].split(';') 70 | u = a[2].split(' ') 71 | obj = {'date': a[1], 'url': u[1]} 72 | urls.append(obj) 73 | # LOGGER.debug(obj) 74 | else: 75 | logging.info('no new urls was found') 76 | else: 77 | LOGGER.critical('apache log file is empty') 78 | # LOGGER.debug(log) 79 | except Exception as e: 80 | LOGGER.critical('Serious Error occured: %s', e) 81 | 82 | return urls 83 | 84 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/execution/CommandsExecutionLifecycle.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.execution; 2 | 3 | import com.google.common.util.concurrent.*; 4 | import com.objectstyle.appfirst.jmx.collector.command.Command; 5 | import com.objectstyle.appfirst.jmx.collector.management.ManagementConnectionFactory; 6 | import com.objectstyle.appfirst.jmx.collector.output.Output; 7 | import com.objectstyle.appfirst.jmx.collector.result.Result; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import java.util.concurrent.Callable; 12 | import java.util.concurrent.ConcurrentMap; 13 | import java.util.concurrent.Executors; 14 | 15 | public class CommandsExecutionLifecycle implements Runnable { 16 | private static final Logger LOGGER = LoggerFactory.getLogger(CommandsExecutionLifecycle.class); 17 | 18 | private static final int DEFAULT_THREAD_POOL_SIZE = 4; 19 | 20 | private final ConcurrentMap commands; 21 | 22 | private final Output output; 23 | 24 | private final ListeningExecutorService executorService; 25 | 26 | private final ManagementConnectionFactory managementConnectionFactory; 27 | 28 | private final FutureCallback commandExecutionCallback = new FutureCallback() { 29 | @Override 30 | public void onSuccess(Result result) { 31 | onCommandExecutionSuccess(result); 32 | } 33 | 34 | @Override 35 | public void onFailure(Throwable t) { 36 | onCommandExecutionFailure(t); 37 | } 38 | }; 39 | 40 | public CommandsExecutionLifecycle(ManagementConnectionFactory managementConnectionFactory, 41 | ConcurrentMap commands, Output output) { 42 | this.managementConnectionFactory = managementConnectionFactory; 43 | this.commands = commands; 44 | this.output = output; 45 | executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE)); 46 | } 47 | 48 | @Override 49 | public void run() { 50 | LOGGER.debug("Starting new command execution cycle"); 51 | LOGGER.debug("There are {} command to execute", commands.size()); 52 | for (final Command command : commands.values()) { 53 | LOGGER.debug("Submitting command for execution: {}", command.toString()); 54 | ListenableFuture future = executorService.submit(new Callable() { 55 | @Override 56 | public Result call() throws Exception { 57 | return new DefaultCommandExecutor(managementConnectionFactory).execute(command); 58 | } 59 | }); 60 | Futures.addCallback(future, commandExecutionCallback); 61 | } 62 | } 63 | 64 | private void onCommandExecutionSuccess(Result result) { 65 | LOGGER.debug("Got execution result: {}", result.toString()); 66 | output.write(result); 67 | } 68 | 69 | private void onCommandExecutionFailure(Throwable t) { 70 | LOGGER.error("Error occurred while executing command", t); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /af.oracle.metrics/src/main/resources/sqls.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | List of SQLs to get metrics from oracle DB 6 | 7 | 8 | 16 | 17 | 26 | 27 | 28 | 29 | 38 | 39 | 40 | 47 | 48 | 51 | 52 | 0 70 | AND ash.is_awr_sample = 'Y' 71 | AND ss.username = '[!USERNAME]' 72 | AND s.parsing_schema_name = '[!USERNAME]' 73 | GROUP BY ash.sql_id, 74 | s.sql_id, 75 | ss.username, 76 | ash.sample_time, 77 | s.elapsed_time, 78 | s.sql_text, 79 | s.executions, 80 | s.last_active_time, 81 | s.COMMAND_TYPE; 82 | ]]> 83 | 84 | 85 | 86 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /apache_log/plugins/options.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | ''' 3 | Options 4 | 5 | ''' 6 | import sys 7 | import logging 8 | import os 9 | import ConfigParser 10 | import argparse 11 | 12 | 13 | LOGGER = logging.getLogger(__name__) 14 | 15 | 16 | class Options(object): 17 | 18 | def __init__(self): 19 | self.options = None 20 | 21 | def setup_logger(self, logger): 22 | # ch = logging.StreamHandler() 23 | level = logging.ERROR 24 | fh = logging.FileHandler('af-apache-log-daemon.log') 25 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 26 | # ch.setFormatter(formatter) 27 | fh.setFormatter(formatter) 28 | 29 | if self.options is not None and self.options.verbose is not None: 30 | level = self.options.verbose 31 | 32 | # print ('set log level to %d ' % level) 33 | logger.setLevel(level) 34 | 35 | logger_components = [logger] 36 | logger_components.append(LOGGER) 37 | logger_components.append(logging.getLogger(name='plugins.apache_url_stat')) 38 | logger_components.append(logging.getLogger(name='plugins.urls_counter')) 39 | logger_components.append(logging.getLogger(name='plugins.app_thread')) 40 | logger_components.append(logging.getLogger(name='plugins.statsd_sender')) 41 | logger_components.append(logging.getLogger(name='plugins.urls_counter')) 42 | 43 | for l in logger_components: 44 | # l.addHandler(ch) 45 | if fh: 46 | l.addHandler(fh) 47 | l.setLevel(level) 48 | 49 | 50 | def get_options(self, parser, config = None): 51 | 52 | parser.add_argument('-v','--verbose', dest='verbose', action='count',default=2, help='Set log level higher you can add multiple') 53 | parser.add_argument('-V','--very_verbose', dest='verbose', action='store_const', const=4, help='Set log level to highest level of detail') 54 | parser.add_argument('-f','--log-to-file', dest='logToFile', help='Store output log to file') 55 | parser.add_argument('-i','--interval', dest='interval', help='Set interval in seconds to run') 56 | parser.add_argument('-t','--tags', dest='tags', help='Enable tags for log file, should be in format like :AF_TAG_NAME:') 57 | parser.add_argument('-a','--apache-host', dest='apacheHostName', help='Apache hostname will be included in statsD counter name') 58 | parser.add_argument('-l','--apache-log-file-path', dest='apacheLogFilePath', help='Path to apache log file') 59 | parser.add_argument('-o','--output-log-file-path', dest='outputFilePath', help='Path to file where urls count will be saved') 60 | 61 | 62 | if config is None: 63 | options = parser.parse_args() 64 | else: 65 | options = parser.parse_args(config) 66 | 67 | level = { 68 | 1: logging.ERROR, 69 | 2: logging.WARNING, 70 | 3: logging.INFO, 71 | 4: logging.DEBUG 72 | }.get(options.verbose, logging.ERROR) 73 | options.verbose = level 74 | 75 | self.options = options 76 | 77 | return options 78 | 79 | #def __init__(self): 80 | # logging.info('creating logger') 81 | 82 | -------------------------------------------------------------------------------- /jmx-collector/src/test/java/com/objectstyle/appfirst/jmx/collector/config/parser/RegexBasedCommandLineParserTest.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config.parser; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.config.CommandLineProcessor; 4 | import org.jmock.Expectations; 5 | import org.jmock.Mockery; 6 | import org.jmock.Sequence; 7 | import org.jmock.auto.Auto; 8 | import org.jmock.auto.Mock; 9 | import org.jmock.integration.junit4.JMock; 10 | import org.jmock.integration.junit4.JUnit4Mockery; 11 | import org.junit.Rule; 12 | import org.junit.Test; 13 | import org.junit.rules.ExpectedException; 14 | import org.junit.runner.RunWith; 15 | 16 | @RunWith(JMock.class) 17 | public class RegexBasedCommandLineParserTest { 18 | private Mockery context = new JUnit4Mockery(); 19 | 20 | @Rule 21 | public ExpectedException thrown = ExpectedException.none(); 22 | 23 | @Mock 24 | private CommandLineProcessor processorMock; 25 | 26 | @Auto 27 | private Sequence processorSequence; 28 | 29 | @Test 30 | public void testFullParseVariant1() throws Exception { 31 | 32 | RegexBasedCommandLineParser parser = new RegexBasedCommandLineParser(processorMock); 33 | 34 | context.checking(new Expectations() { 35 | { 36 | oneOf(processorMock).startCommand("nycstgpjbsapp01-as1-NonHeapMemoryUsage"); 37 | inSequence(processorSequence); 38 | 39 | oneOf(processorMock).startMBeanName("java.lang:type=Memory"); 40 | inSequence(processorSequence); 41 | 42 | oneOf(processorMock).startMBeanAttribute("NonHeapMemoryUsage"); 43 | inSequence(processorSequence); 44 | 45 | oneOf(processorMock).startVirtualMachineMatchPattern("as1"); 46 | inSequence(processorSequence); 47 | 48 | oneOf(processorMock).startMBeanAttributeKey("used"); 49 | inSequence(processorSequence); 50 | 51 | oneOf(processorMock).endCommand(); 52 | inSequence(processorSequence); 53 | } 54 | }); 55 | parser.parse("jmx_command[nycstgpjbsapp01-as1-NonHeapMemoryUsage] -P as1 -O java.lang:type=Memory -A NonHeapMemoryUsage -K used"); 56 | } 57 | 58 | @Test 59 | public void testFullParseVariant2() throws Exception { 60 | 61 | RegexBasedCommandLineParser parser = new RegexBasedCommandLineParser(processorMock); 62 | 63 | context.checking(new Expectations() { 64 | { 65 | oneOf(processorMock).startCommand("nycstgpjbsapp01-as1-ThreadPool"); 66 | inSequence(processorSequence); 67 | 68 | oneOf(processorMock).startMBeanName("jboss.web:type=ThreadPool,name=ajp-nycstgpjbsapp01-as1 172.16.78.152-8009"); 69 | inSequence(processorSequence); 70 | 71 | oneOf(processorMock).startMBeanAttribute("currentThreadsBusy"); 72 | inSequence(processorSequence); 73 | 74 | oneOf(processorMock).startVirtualMachineMatchPattern("as1"); 75 | inSequence(processorSequence); 76 | 77 | oneOf(processorMock).endCommand(); 78 | inSequence(processorSequence); 79 | } 80 | }); 81 | parser.parse("jmx_command[nycstgpjbsapp01-as1-ThreadPool] -P as1 -O jboss.web:type=ThreadPool,name=ajp-nycstgpjbsapp01-as1 172.16.78.152-8009 -A currentThreadsBusy"); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/result/MBeanDataConverterFactory.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.result; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.result.converter.CompositeTypeConverter; 4 | import com.objectstyle.appfirst.jmx.collector.result.converter.MapConverter; 5 | import com.objectstyle.appfirst.jmx.collector.result.converter.SetConverter; 6 | import com.objectstyle.appfirst.jmx.collector.result.converter.ToStringConverter; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | import java.util.Map; 11 | import java.util.Set; 12 | 13 | import static java.lang.String.format; 14 | 15 | public enum MBeanDataConverterFactory { 16 | INSTANCE; 17 | 18 | private static final Logger LOGGER = LoggerFactory.getLogger(CompositeTypeConverter.class); 19 | 20 | @SuppressWarnings("unchecked") 21 | public MBeanDataConverter getConverter(String typeClassName) throws UnsupportedDataTypeException { 22 | 23 | if (Primitives.isPrimitiveOrWrapperType(classForName(typeClassName))) { 24 | LOGGER.debug(format("Converting result from primitive '%s' type", typeClassName)); 25 | return new ToStringConverter(); 26 | } 27 | else if (typeClassName.equals(Map.class.getName())) { 28 | LOGGER.debug(format("Converting result from '%s' type", typeClassName)); 29 | return new MapConverter(); 30 | } 31 | else if (typeClassName.equals(Set.class.getName())) { 32 | LOGGER.debug(format("Converting result from '%s' type", typeClassName)); 33 | return new SetConverter(); 34 | } 35 | else { 36 | throw new UnsupportedDataTypeException(format("Unsupported data type '%s'", typeClassName)); 37 | } 38 | } 39 | 40 | private Class classForName(String fullyQualifiedClassName) throws UnsupportedDataTypeException { 41 | try { 42 | return Class.forName(fullyQualifiedClassName); 43 | } 44 | catch (ClassNotFoundException e) { 45 | if (fullyQualifiedClassName.equals("int")) { 46 | return int.class; 47 | } 48 | else if (fullyQualifiedClassName.equals("boolean")) { 49 | return boolean.class; 50 | 51 | } 52 | else if (fullyQualifiedClassName.equals("char")) { 53 | return char.class; 54 | 55 | } 56 | else if (fullyQualifiedClassName.equals("byte")) { 57 | return byte.class; 58 | 59 | } 60 | else if (fullyQualifiedClassName.equals("short")) { 61 | return short.class; 62 | 63 | } 64 | else if (fullyQualifiedClassName.equals("double")) { 65 | return double.class; 66 | 67 | } 68 | else if (fullyQualifiedClassName.equals("long")) { 69 | return long.class; 70 | 71 | } 72 | else if (fullyQualifiedClassName.equals("boolean")) { 73 | return boolean.class; 74 | 75 | } 76 | else if (fullyQualifiedClassName.equals("void")) { 77 | return void.class; 78 | 79 | } 80 | else if (fullyQualifiedClassName.equals("float")) { 81 | return float.class; 82 | 83 | } 84 | throw new UnsupportedDataTypeException(fullyQualifiedClassName); 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /check_zookeeper.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Author: Tony Ling 5 | Executes a zookeeper 4 letter command and parses the output depending on the metric argument. 6 | The script uses netcat 'nc' command. For use with AppFirst collector. 7 | 8 | Example Usage: 9 | python check_zookeeper.py -t connections -H localhost -P 2181 -W 100 -C 200 10 | 11 | Created on: 8/26/14 12 | """ 13 | 14 | import sys 15 | import subprocess 16 | import argparse 17 | 18 | status_code = 3; 19 | 20 | # metrics dict keys are script metric -t arguments, key values are array values 21 | # Elements: 1=nc command to zk server, 2=metric to obtain, 3=script output string 22 | metrics = {} 23 | metrics['mode'] = ['stat', 'Mode', 'Server Mode'] 24 | metrics['connections'] = ['stat', 'Connections', 'Number of client connections'] 25 | metrics['connections_outstanding'] = ['stat', 'Outstanding', 'Number of outstanding requests'] 26 | metrics['zk_znode_count']= ['mntr', 'zk_znode_count', 'Zookeeper Znode count'] 27 | metrics['zk_min_latency']= ['mntr', 'zk_min_latency', 'Zookeeper min latency'] 28 | metrics['zk_max_latency']= ['mntr', 'zk_max_latency', 'Zookeeper max latency'] 29 | metrics['zk_avg_latency']= ['mntr', 'zk_avg_latency', 'Zookeeper avg latency'] 30 | 31 | parser = argparse.ArgumentParser() 32 | parser.add_argument('-H', '--host', help='Host location, default=localhost', default='localhost') 33 | parser.add_argument('-P', '--port', help='Port number, default=2181', default='2181') 34 | parser.add_argument('-W', '--warning', help='Warning threshold', default=None, type=float) 35 | parser.add_argument('-C', '--critical', help='Critical theshold', default=None, type=float) 36 | parser.add_argument('-t', '--metric', help='Metric to choose', default=None, required=True) 37 | 38 | args = parser.parse_args() 39 | 40 | if (args.critical and args.critical < 0): 41 | print "Error: -C Critical cannot be negative" 42 | sys.exit(status_code) 43 | elif (args.warning and args.warning < 0): 44 | print "Error: -W Warning cannot be negative" 45 | sys.exit(status_code) 46 | try: 47 | array_value = metrics[args.metric] 48 | cmd = array_value[0] 49 | metric = array_value[1] 50 | msg = array_value[2] 51 | zk_info = subprocess.Popen(['nc', args.host, args.port], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 52 | output, err = zk_info.communicate(input=cmd) 53 | except KeyError: 54 | print "Error: Unrecognized/unsupported metric '{_metric}'".format(_metric=args.metric) 55 | str_metrics = "Supported Metrics: [" 56 | for metric in metrics: 57 | str_metrics += ("'" + metric + "',") 58 | print str_metrics[:-1] + "]" 59 | sys.exit(status_code) 60 | 61 | for line in output.split('\n'): 62 | if (cmd == 'stat'): 63 | tokens = line.split(':') 64 | elif (cmd == 'mntr'): 65 | tokens = line.split('\t') 66 | else: 67 | break 68 | if (tokens[0] == metric): 69 | try: 70 | if (args.critical and float(tokens[1]) >= args.critical): 71 | print "CRITICAL - " + msg + ": " + tokens[1] + "|" + metric + "=" + tokens[1] 72 | status_code = 2 73 | elif (args.warning and float(tokens[1]) >= args.warning): 74 | print "WARNING - " + msg + ": " + tokens[1] + "|" + metric + "=" + tokens[1] 75 | status_code = 1 76 | else: 77 | print "OK - " + msg + ": " + tokens[1] + "|" + metric + "=" + tokens[1] 78 | status_code = 0 79 | except ValueError: 80 | print "OK - " + msg + ": " + tokens[1] + "|" + metric + "=" + tokens[1] 81 | status_code = 0 82 | break 83 | 84 | sys.exit(status_code) 85 | -------------------------------------------------------------------------------- /af.oracle.metrics/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.springframework 8 | af.oracle.metrics 9 | 0.1.0 10 | 11 | 12 | 13 | log4j 14 | log4j 15 | 1.2.17 16 | 17 | 18 | 19 | 20 | com.oracle 21 | ojdbc6 22 | 11.2.0.2.0 23 | 24 | 25 | 26 | commons-cli 27 | commons-cli 28 | 1.1 29 | 30 | 31 | org.apache.commons 32 | commons-lang3 33 | 3.0 34 | 35 | 36 | 41 | 42 | 43 | 44 | 45 | com.os.appfirst.Application 46 | UTF-8 47 | 48 | 49 | 50 | 51 | 52 | 60 | 61 | 62 | 63 | org.apache.maven.plugins 64 | maven-assembly-plugin 65 | 66 | 67 | 68 | true 69 | lib/ 70 | com.os.appfirst.App 71 | 72 | 73 | 74 | jar-with-dependencies 75 | 76 | 77 | 78 | 79 | make-assembly 80 | package 81 | 82 | single 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | codelds 93 | https://code.lds.org/nexus/content/groups/main-repo 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /check_cassandra.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Author: Tony Ling 5 | Executes the nodetool script located within the cassandra bin directory 6 | and parses the output depending on the metric argument. 7 | The script requires the path of the nodetool script that comes with 8 | apache-cassandra. By default, it is located in the bin directory of 9 | apache-cassandra. 10 | For use with AppFirst collector 11 | 12 | Usage Example: 13 | python /usr/share/appfirst/plugins/libexec/check_cassandra.py -L path/to/cassandra/bin -t pending_tasks -W 50 -C 100 14 | 15 | Created on: 8/22/14 16 | """ 17 | 18 | import sys 19 | import subprocess 20 | import argparse 21 | 22 | status_code = 3 23 | value = 0 24 | 25 | # metrics dict key is script argument 26 | # values are array of metric to filter for and output string 27 | metrics = {} 28 | metrics['bloom_filter_space_used'] = ['Bloom Filter Space Used', 'Total Bloom Filter Space Used'] 29 | metrics['bloom_filter_false_positives'] = ['Bloom Filter False Positives', 'Total Bloom Filter False Positives'] 30 | metrics['bloom_filter_false_ratio'] = ['Bloom Filter False Ratio', 'Total Bloom Filter False Ratio'] 31 | metrics['pending_tasks'] = ['Pending Tasks', 'Total Pending Tasks'] 32 | metrics['memtable_switch_count'] = ['Memtable Switch Count', 'Total Switch Count'] 33 | metrics['memtable_data_size'] = ['Memtable Data Size', 'Total Data Size'] 34 | metrics['memtable_columns_count'] = ['Memtable Columns Count', 'Total Columns Count'] 35 | 36 | parser = argparse.ArgumentParser() 37 | parser.add_argument('-L', '--location', help='Directory path of nodetool script, should be in same directory as cassdandra') 38 | parser.add_argument('-W', '--warning', help='Warning threshold', default=None, type=float) 39 | parser.add_argument('-C', '--critical', help='Critical theshold', default=None, type=float) 40 | parser.add_argument('-t', '--metric', help='Metric to choose', default=None, required=True) 41 | 42 | args = parser.parse_args() 43 | if (args.location): 44 | path = args.location + "/nodetool" 45 | else: 46 | print "Error - option -L for NodeTool script directory required" 47 | sys.exit(2) 48 | try: 49 | metric = args.metric 50 | cmd = metrics[metric][0] 51 | msg = metrics[metric][1] 52 | 53 | except KeyError: 54 | print "Error: Unrecognized/unsupported metric '{_metric}'".format(_metric=args.metric) 55 | str_metrics = "Supported Metrics: [" 56 | for metric in metrics: 57 | str_metrics += ("'" + metric + "',") 58 | print str_metrics[:-1] + "]" 59 | sys.exit(status_code) 60 | if (args.critical and args.critical < 0): 61 | print "Error: -C Critical cannot be negative" 62 | sys.exit(status_code) 63 | elif (args.warning and args.warning < 0): 64 | print "Error: -W Warning cannot be negative" 65 | sys.exit(status_code) 66 | 67 | cass_info = subprocess.Popen([path, 'cfstats'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 68 | output, err = cass_info.communicate() 69 | if (output is ""): 70 | sys.exit(status_code) 71 | for line in output.split('\n\t\t'): 72 | tokens = line.split(':') 73 | if (tokens[0] == cmd): 74 | value += float(tokens[1]) 75 | 76 | 77 | if (args.critical and value>= args.critical): 78 | print "CRITICAL - " + msg + ": " + str(value) + "|" + metric + "=" + str(value) 79 | status_code = 2 80 | elif (args.warning and value >= args.warning): 81 | print "WARNING - " + msg + ": " + str(value) + "|" + metric + "=" + str(value) 82 | status_code = 1 83 | else: 84 | print "OK - " + msg + ": " + str(value) + "|" + metric + "=" + str(value) 85 | status_code = 0 86 | 87 | sys.exit(status_code) 88 | -------------------------------------------------------------------------------- /apache_log/plugins/statsd_sender.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import sys 3 | import logging 4 | import os 5 | import re 6 | import threading 7 | import pickle 8 | 9 | 10 | LOGGER = logging.getLogger(__name__) 11 | 12 | 13 | class StatsdSender(threading.Thread): 14 | 15 | def __init__(self, apacheHostName = None, pklData = None): 16 | super(StatsdSender, self).__init__() 17 | 18 | LOGGER.debug('init StatsdSender') 19 | 20 | 21 | self.urlsSumm = None 22 | self.urls = None 23 | self.pklData = pklData 24 | 25 | try: 26 | if self.pklData is None: 27 | pklFile = open('/tmp/data.pkl', 'rb') 28 | self.pklData = pickle.load(pklFile) 29 | pklFile.close() 30 | 31 | LOGGER.info('reading pickle data') 32 | 33 | if self.pklData is not None: 34 | 35 | self.urlsSumm = self.pklData['urlsSumm'] 36 | self.urls = self.pklData['urls'] 37 | 38 | 39 | except Exception as e: 40 | LOGGER.critical('Serious Error occured: %s', e) 41 | 42 | self.running = True 43 | self.stoprequest = threading.Event() 44 | LOGGER.info('creating StatsdSender') 45 | self.apacheHostName = apacheHostName 46 | 47 | 48 | def getBaseName(self): 49 | name = '' 50 | if self.apacheHostName is not None: 51 | name = self.apacheHostName + '.' 52 | 53 | return name 54 | 55 | def convertUrlToName(self, url): 56 | 57 | url = url.replace ('http://', '') 58 | url = url.replace ('https://', '') 59 | url = url.replace (' ', '') 60 | index = url.find('?') 61 | if index > 0: 62 | url = url[0:index] 63 | 64 | if url[0] is '/': 65 | url = url[1:] 66 | return url 67 | 68 | def increment(self, statsd, name, val): 69 | 70 | while val > 0: 71 | statsd.increment(name) 72 | val = val - 1 73 | 74 | def sendCountOfUrls(self, Statsd, urls): 75 | 76 | LOGGER.debug('sendCountOfUrls') 77 | 78 | for key, val in urls.iteritems(): 79 | if key is not '*': 80 | name = self.getBaseName() + self.convertUrlToName(key) 81 | LOGGER.debug('converting url ' + key + ' to counter name ' + name) 82 | LOGGER.debug('setting counter ' + name + ' to %d' % val['count']) 83 | self.increment(Statsd, name, val['count']) 84 | 85 | def sendSummOfUrls(self, Statsd, urls): 86 | name = self.getBaseName() 87 | Statsd.gauge('af_url_counter.' + name, len(urls)) 88 | 89 | def run(self): 90 | 91 | try: 92 | 93 | LOGGER.debug('run StatsdSender') 94 | from afstatsd import Statsd, AFTransport 95 | # Statsd.set_transport(AFTransport(logger=LOGGER, verbosity=True)) 96 | 97 | if self.urlsSumm is not None: 98 | self.sendCountOfUrls(Statsd, self.urlsSumm) 99 | 100 | if self.urls is not None: 101 | self.sendSummOfUrls(Statsd, self.urls) 102 | 103 | if self.isAlive(): 104 | LOGGER.debug('statsD thread NOT stoped') 105 | else: 106 | LOGGER.debug('statsD thread stoped') 107 | 108 | except Exception as e: 109 | LOGGER.critical('Serious Error occured: %s', e) 110 | 111 | 112 | def join(self, timeout=None): 113 | self.stoprequest.set() 114 | self.running = None 115 | super(StatsdSender, self).join(timeout) 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /jmx-collector/src/test/java/com/objectstyle/appfirst/jmx/collector/config/CommandBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.Command; 4 | import com.objectstyle.appfirst.jmx.collector.command.CompositeValueDefinition; 5 | import com.objectstyle.appfirst.jmx.collector.command.PatternVirtualMachineDefinition; 6 | import com.objectstyle.appfirst.jmx.collector.command.ValueDefinition; 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.ExpectedException; 10 | 11 | import static org.junit.Assert.assertEquals; 12 | import static org.junit.Assert.assertTrue; 13 | 14 | public class CommandBuilderTest { 15 | @Rule 16 | public ExpectedException thrown = ExpectedException.none(); 17 | 18 | @Test 19 | public void testBuildCommandWithSimpleValueDefinition() throws Exception { 20 | CommandBuilder commandBuilder = new CommandBuilder(); 21 | commandBuilder.startCommand("Application.Runtime.Name"); 22 | commandBuilder.startVirtualMachineMatchPattern("com.objectstyle.Application"); 23 | commandBuilder.startMBeanName("java.lang:type=Runtime"); 24 | commandBuilder.startMBeanAttribute("name"); 25 | commandBuilder.endCommand(); 26 | Command command = commandBuilder.buildCommand(); 27 | 28 | assertEquals("Application.Runtime.Name", command.getName()); 29 | assertTrue(command.getVirtualMachineDefinition() instanceof PatternVirtualMachineDefinition); 30 | assertEquals("com.objectstyle.Application", 31 | ((PatternVirtualMachineDefinition) command.getVirtualMachineDefinition()).getMatchPattern()); 32 | assertEquals(ValueDefinition.class, command.getValueDefinition().getClass()); 33 | assertEquals("java.lang:type=Runtime", command.getValueDefinition().getName()); 34 | assertEquals("name", command.getValueDefinition().getAttribute()); 35 | } 36 | 37 | @Test 38 | public void testBuildCommandWithCompositeValueDefinition() throws Exception { 39 | CommandBuilder commandBuilder = new CommandBuilder(); 40 | commandBuilder.startCommand("Application.Memory.HeapMemoryUsage.Used"); 41 | commandBuilder.startVirtualMachineMatchPattern("com.objectstyle.Application"); 42 | commandBuilder.startMBeanName("java.lang:type=Memory"); 43 | commandBuilder.startMBeanAttribute("heapMemoryUsage"); 44 | commandBuilder.startMBeanAttributeKey("used"); 45 | commandBuilder.endCommand(); 46 | Command command = commandBuilder.buildCommand(); 47 | 48 | assertEquals("Application.Memory.HeapMemoryUsage.Used", command.getName()); 49 | assertTrue(command.getVirtualMachineDefinition() instanceof PatternVirtualMachineDefinition); 50 | assertEquals("com.objectstyle.Application", 51 | ((PatternVirtualMachineDefinition) command.getVirtualMachineDefinition()).getMatchPattern()); 52 | assertEquals(CompositeValueDefinition.class, command.getValueDefinition().getClass()); 53 | assertEquals("java.lang:type=Memory", command.getValueDefinition().getName()); 54 | assertEquals("heapMemoryUsage", command.getValueDefinition().getAttribute()); 55 | assertEquals("used", ((CompositeValueDefinition) command.getValueDefinition()).getAttributeKey()); 56 | } 57 | 58 | @Test 59 | public void testThrowExceptionOnNotBuiltCommand() throws Exception { 60 | CommandBuilder builder = new CommandBuilder(); 61 | 62 | thrown.expect(IllegalStateException.class); 63 | thrown.expectMessage("Command has not been built yet"); 64 | 65 | builder.startCommand("test"); 66 | builder.buildCommand(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /apache_log/plugins/daemon.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys, os, time, atexit 4 | from signal import SIGTERM 5 | 6 | class Daemon(object): 7 | """ 8 | A generic daemon class. 9 | 10 | Usage: subclass the Daemon class and override the run() method 11 | """ 12 | def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): 13 | self.stdin = stdin 14 | self.stdout = stdout 15 | self.stderr = stderr 16 | self.pidfile = pidfile 17 | 18 | 19 | 20 | def daemonize(self): 21 | """ 22 | do the UNIX double-fork magic, see Stevens' "Advanced 23 | Programming in the UNIX Environment" for details (ISBN 0201563177) 24 | http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 25 | """ 26 | try: 27 | pid = os.fork() 28 | if pid > 0: 29 | # exit first parent 30 | sys.exit(0) 31 | except OSError, e: 32 | sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) 33 | sys.exit(1) 34 | 35 | # decouple from parent environment 36 | os.chdir("/") 37 | os.setsid() 38 | os.umask(0) 39 | 40 | # do second fork 41 | try: 42 | pid = os.fork() 43 | if pid > 0: 44 | # exit from second parent 45 | sys.exit(0) 46 | except OSError, e: 47 | sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) 48 | sys.exit(1) 49 | 50 | # redirect standard file descriptors 51 | sys.stdout.flush() 52 | sys.stderr.flush() 53 | si = file(self.stdin, 'r') 54 | so = file(self.stdout, 'a+') 55 | se = file(self.stderr, 'a+', 0) 56 | os.dup2(si.fileno(), sys.stdin.fileno()) 57 | os.dup2(so.fileno(), sys.stdout.fileno()) 58 | os.dup2(se.fileno(), sys.stderr.fileno()) 59 | 60 | # write pidfile 61 | atexit.register(self.delpid) 62 | pid = str(os.getpid()) 63 | file(self.pidfile,'w+').write("%s\n" % pid) 64 | 65 | def delpid(self): 66 | os.remove(self.pidfile) 67 | 68 | def start(self): 69 | """ 70 | Start the daemon 71 | """ 72 | # Check for a pidfile to see if the daemon already runs 73 | try: 74 | pf = file(self.pidfile,'r') 75 | pid = int(pf.read().strip()) 76 | pf.close() 77 | except IOError: 78 | pid = None 79 | 80 | if pid: 81 | message = "pidfile %s already exist. Daemon already running?\n" 82 | sys.stderr.write(message % self.pidfile) 83 | sys.exit(1) 84 | 85 | # Start the daemon 86 | self.daemonize() 87 | self.run() 88 | 89 | def stop(self): 90 | """ 91 | Stop the daemon 92 | """ 93 | 94 | # Get the pid from the pidfile 95 | try: 96 | pf = file(self.pidfile,'r') 97 | pid = int(pf.read().strip()) 98 | pf.close() 99 | except IOError: 100 | print 'IOError ' + self.pidfile 101 | pid = None 102 | 103 | if not pid: 104 | message = "pidfile %s does not exist. Daemon not running?\n" 105 | sys.stderr.write(message % self.pidfile) 106 | return # not an error in a restart 107 | 108 | # Try killing the daemon process 109 | try: 110 | while 1: 111 | os.kill(pid, SIGTERM) 112 | time.sleep(0.1) 113 | # if os.path.exists(self.pidfile): 114 | # print 'removing pid file ' + self.pidfile 115 | # os.remove(self.pidfile) 116 | except OSError, err: 117 | err = str(err) 118 | if err.find("No such process") > 0: 119 | if os.path.exists(self.pidfile): 120 | os.remove(self.pidfile) 121 | else: 122 | print str(err) 123 | sys.exit(1) 124 | 125 | def restart(self): 126 | """ 127 | Restart the daemon 128 | """ 129 | self.stop() 130 | self.start() 131 | 132 | def run(self): 133 | """ 134 | You should override this method when you subclass Daemon. It will be called after the process has been 135 | daemonized by start() or restart(). 136 | """ 137 | 138 | 139 | -------------------------------------------------------------------------------- /test/test_nagios.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on May 30, 2012 3 | 4 | @author: yangming 5 | ''' 6 | import unittest 7 | import nagios 8 | import statsd 9 | import sys 10 | from StringIO import StringIO 11 | import mock 12 | 13 | class BasePluginMock(nagios.BasePlugin): 14 | def check(self, request): 15 | pass 16 | 17 | class TestNagios(unittest.TestCase): 18 | def test_rootify(self): 19 | mock_os = mock.Mock() 20 | # sys.modules['os'] = mock_os 21 | nagios.__dict__['os'] = mock_os 22 | mock_os.geteuid.return_value = 1 23 | self.assertEqual("sudo ls", nagios.rootify("ls")) 24 | self.assertEqual("sudo ls", nagios.rootify("sudo ls")) 25 | self.assertEqual("sudo -u appfirst ls", nagios.rootify("ls", "appfirst")) 26 | self.assertEqual("sudo -u appfirst ls", nagios.rootify("sudo ls", "appfirst")) 27 | mock_os.geteuid.return_value = 0 28 | self.assertEqual("echo \"rosie\"", nagios.rootify("echo \"rosie\"")) 29 | self.assertEqual("sudo echo \"rosie\"", nagios.rootify("sudo echo \"rosie\"")) 30 | self.assertEqual("su -l appfirst -c \"echo \\\"rosie\\\"\"", 31 | nagios.rootify("echo \"rosie\"", "appfirst")) 32 | 33 | 34 | class TestStatsd(unittest.TestCase): 35 | def statsd_decorator(self, method): 36 | self.assertTrue(method is not None, "empty method returned") 37 | return method; 38 | 39 | def test_statsd_gauge(self): 40 | @self.statsd_decorator 41 | @statsd.gauge 42 | def testfunc1(*args, **kwargs): 43 | result = nagios.Result("GAUGE_COMMAND", nagios.Status.OK) 44 | result.add_performance_data("total", 10) 45 | return result 46 | 47 | def test_statsd_counter(self): 48 | @self.statsd_decorator 49 | @statsd.counter 50 | def testfunc2(*arg, **kwargs): 51 | result = nagios.Result("COUNTER_COMMAND", nagios.Status.OK) 52 | result.add_performance_data("total", 10) 53 | return result 54 | 55 | def test_statsd_timer(self): 56 | @self.statsd_decorator 57 | @statsd.timer 58 | def testfunc3(*arg, **kwargs): 59 | result = nagios.Result("TIMER_COMMAND", nagios.Status.OK) 60 | result.add_performance_data("total", 10) 61 | return result 62 | 63 | class TestBasePlugin(unittest.TestCase): 64 | def setUp(self): 65 | self.original_stdout, sys.stdout = sys.stdout, StringIO() 66 | 67 | def tearDown(self): 68 | sys.stdout.close() 69 | sys.stdout = self.original_stdout 70 | 71 | def test_result_str(self): 72 | # a result w/out pf data 73 | r = nagios.Result('SLOW_QUERIES', nagios.Status.OK, '2 queries', 'MYSQL') 74 | expected = 'SLOW_QUERIES OK: 2 queries' 75 | self.assertEqual(expected, str(r)) 76 | # one performance data added 77 | r.add_performance_data('slow_queries', 2, warn=10, crit=20) 78 | expected = 'SLOW_QUERIES OK: 2 queries | slow_queries=2;10;20' 79 | self.assertEqual(expected, str(r)) 80 | # two performance data added 81 | r.add_performance_data('slow_queries_rate', 0.2, warn=0.5, crit=1) 82 | expected = 'SLOW_QUERIES OK: 2 queries | slow_queries=2;10;20 slow_queries_rate=0.2;0.5;1' 83 | self.assertEqual(expected, str(r)) 84 | 85 | def test_verdict(self): 86 | ba = BasePluginMock() 87 | self.assertEqual(nagios.Status.OK, ba.verdict(2, 6, 8)) 88 | self.assertEqual(nagios.Status.OK, ba.verdict(2, 5, None)) 89 | self.assertEqual(nagios.Status.OK, ba.verdict(2, None, None)) 90 | 91 | if __name__ == "__main__": 92 | #import sys;sys.argv = ['', 'Test.test_'] 93 | unittest.main() -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/output/af/AfCollector.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.output.af; 2 | 3 | import com.sun.jna.Library; 4 | import com.sun.jna.Native; 5 | 6 | public class AfCollector { 7 | 8 | private static String AFCollectorAPIName = "/afcollectorapi"; 9 | private static String LibName = "rt"; 10 | private static int O_WRONLY = 01; 11 | private static int APICollectorMaxMsgSize = 2048; 12 | 13 | public enum AFCollectorMsgSeverity { 14 | AFCSeverityInformation, 15 | AFCSeverityWarning, 16 | AFCSeverityCritical, 17 | AFCSeverityStatsd, 18 | AFCSeverityPolled; 19 | 20 | public static int toInt(AFCollectorMsgSeverity en) { 21 | switch (en) { 22 | case AFCSeverityInformation: { 23 | return 0; 24 | } 25 | case AFCSeverityWarning: { 26 | return 1; 27 | } 28 | case AFCSeverityCritical: { 29 | return 2; 30 | } 31 | case AFCSeverityStatsd: { 32 | return 3; 33 | } 34 | case AFCSeverityPolled: { 35 | return 4; 36 | } 37 | } 38 | return -1; 39 | 40 | } 41 | } 42 | 43 | public enum AFCollectorReturnCode { 44 | AFCSuccess, 45 | AFCNoMemory, 46 | AFCBadParam, 47 | AFCOpenError, 48 | AFCPostError, 49 | AFCWouldBlock, 50 | AFCCloseError; 51 | 52 | public static AFCollectorReturnCode fromInt(int i) { 53 | switch (i) { 54 | case 0: { 55 | return AFCSuccess; 56 | } 57 | case 1: { 58 | return AFCNoMemory; 59 | } 60 | case 2: { 61 | return AFCBadParam; 62 | } 63 | case 3: { 64 | return AFCOpenError; 65 | } 66 | case 4: { 67 | return AFCPostError; 68 | } 69 | case 5: { 70 | return AFCWouldBlock; 71 | } 72 | case 6: { 73 | return AFCCloseError; 74 | } 75 | } 76 | return AFCSuccess; 77 | } 78 | } 79 | 80 | public interface CLibrary extends Library { 81 | CLibrary INSTANCE = (CLibrary) 82 | Native.loadLibrary("c", CLibrary.class); 83 | 84 | int getpid(); 85 | } 86 | 87 | public interface MQ extends Library { 88 | public int mq_open(String filename, int mode); 89 | 90 | public int mq_close(int mqd); 91 | 92 | public int mq_send(int mqd, String msg, int len, int prio); 93 | } 94 | 95 | 96 | public static AFCollectorReturnCode postAFCollectorMessage(AFCollectorMsgSeverity severity, String msg) { 97 | int len = msg.length(); 98 | if (len > APICollectorMaxMsgSize) { 99 | return AFCollectorReturnCode.AFCBadParam; 100 | } 101 | 102 | int pid = CLibrary.INSTANCE.getpid(); 103 | int prio = AFCollectorMsgSeverity.toInt(severity); 104 | int rv = 0; 105 | 106 | MQ mq = (MQ) Native.loadLibrary(LibName, MQ.class); 107 | 108 | int mqd = mq.mq_open(AFCollectorAPIName, O_WRONLY); 109 | 110 | String incident = pid + ":" + msg; 111 | len = incident.length(); 112 | 113 | rv = mq.mq_send(mqd, incident, len, prio); 114 | 115 | mq.mq_close(mqd); 116 | 117 | return AFCollectorReturnCode.fromInt(rv); 118 | } 119 | } 120 | 121 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/resolve/CachingLocalVirtualMachineResolver.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.resolve; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.PatternVirtualMachineDefinition; 4 | import com.objectstyle.appfirst.jmx.collector.command.URLVirtualMachineDefinition; 5 | import com.objectstyle.appfirst.jmx.collector.command.VirtualMachineDefinition; 6 | import com.sun.tools.attach.VirtualMachine; 7 | import com.sun.tools.attach.VirtualMachineDescriptor; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import java.util.HashMap; 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | public class CachingLocalVirtualMachineResolver 16 | implements VirtualMachineResolver, VirtualMachineIdentifierInvalidationListener { 17 | private static final Logger LOGGER = LoggerFactory.getLogger(CachingLocalVirtualMachineResolver.class); 18 | 19 | private Map cachedIdentifiers 20 | = new HashMap(); 21 | 22 | private LocalVirtualMachineResolver localVirtualMachineResolver 23 | = new LocalVirtualMachineResolver(new JvmstatVirtualMachineMatcher()); 24 | 25 | private URLVirtualMachineResolver urlVirtualMachineResolver = new URLVirtualMachineResolver(); 26 | 27 | @Override 28 | public synchronized VirtualMachineIdentifier resolve(VirtualMachineDefinition definition) 29 | throws VirtualMachineResolverException { 30 | LOGGER.debug("Resolving descriptor for the virtual machine definition {}", definition); 31 | if (cachedIdentifiers.containsKey(definition)) { 32 | VirtualMachineIdentifier identifier = cachedIdentifiers.get(definition); 33 | LOGGER.debug("Found cached identifier {} for the virtual machine definition {}", identifier, definition); 34 | if (!(identifier instanceof LocalVirtualMachineIdentifier)) { 35 | return identifier; 36 | } else { 37 | 38 | if (checkVirtualMachineExists(((LocalVirtualMachineIdentifier) identifier).getDescriptor())) { 39 | LOGGER.debug("Virtual machine with identifier {} still exists. Returning it.", identifier); 40 | return identifier; 41 | } else { 42 | LOGGER.debug("Virtual machine with identifier {} does not exist. Invalidating identifier.", identifier); 43 | identifier.invalidate(); 44 | } 45 | } 46 | } 47 | 48 | VirtualMachineIdentifier identifier; 49 | if (definition instanceof PatternVirtualMachineDefinition) { 50 | LOGGER.debug("Delegating resolve for the definition {} to the local resolver", definition); 51 | identifier = localVirtualMachineResolver.resolve(definition); 52 | } else if (definition instanceof URLVirtualMachineDefinition) { 53 | LOGGER.debug("Delegating resolve for the definition {} to the URL resolver", definition); 54 | identifier = urlVirtualMachineResolver.resolve(definition); 55 | } else { 56 | throw new IllegalArgumentException("Unknown definition type"); 57 | } 58 | cachedIdentifiers.put(definition, identifier); 59 | return identifier; 60 | } 61 | 62 | @SuppressWarnings("SuspiciousMethodCalls") 63 | @Override 64 | public void identifierInvalidated(VirtualMachineIdentifier identifier) { 65 | cachedIdentifiers.remove(identifier); 66 | } 67 | 68 | private boolean checkVirtualMachineExists(VirtualMachineDescriptor descriptor) { 69 | List availableDescriptors = VirtualMachine.list(); 70 | return availableDescriptors.contains(descriptor); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /check_couchdb.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Author: Tony Ling 5 | Checks couch _stats page and parses the output depending on the argument. 6 | For use with AppFirst collector 7 | 8 | Example Usage: python /usr/share/appfirst/plugins/libexec/check_couchdb.py -t average_requests -W 50 -C 100 9 | 10 | Created on: 8/22/14 11 | """ 12 | 13 | import sys 14 | import argparse 15 | import urllib2 16 | import json 17 | 18 | status_code = 3 19 | 20 | # metrics dict key value is an array of 4 strings. 21 | # Elements: 0=part 1 of url pth, 1=part 2 of url path, 3=json key, 4=message 22 | metrics = {} 23 | metrics['total_requests'] = ['httpd','requests','current','Total httpd requests since server started'] 24 | metrics['average_requests'] = ['httpd','requests','mean','Httpd requests per second'] 25 | metrics['total_bulk_requests'] = ['httpd','bulk_requests','current','Total httpd bulk requests since server started'] 26 | metrics['average_bulk_requests'] = ['httpd','bulk_requests','mean','Httpd bulk requests per second'] 27 | metrics['average_COPY_requests'] = ['httpd_request_methods','COPY','mean','Httpd COPY requests per second'] 28 | metrics['average_DELETE_requests'] = ['httpd_request_methods','DELETE','mean','Httpd DELETE requests per second'] 29 | metrics['average_GET_requests'] = ['httpd_request_methods','GET','mean','Httpd GET requests per second'] 30 | metrics['average_HEAD_requests'] = ['httpd_request_methods','HEAD','mean','Httpd HEAD requests per second'] 31 | metrics['average_MOVE_requests'] = ['httpd_request_methods','MOVE','mean','Httpd MOVE requests per second'] 32 | metrics['average_POST_requests'] = ['httpd_request_methods','POST','mean','Httpd POST requests per second'] 33 | metrics['average_PUT_requests'] = ['httpd_request_methods','PUT','mean','Httpd PUT requests per second'] 34 | 35 | parser = argparse.ArgumentParser() 36 | parser.add_argument('-H', '--host', help='Host location, default=localhost', default='localhost') 37 | parser.add_argument('-P', '--port', help='Port number, default=5984', default='5984') 38 | parser.add_argument('-W', '--warning', help='Warning threshold', default=None, type=float) 39 | parser.add_argument('-C', '--critical', help='Critical theshold', default=None, type=float) 40 | parser.add_argument('-t', '--metric', help='Metric to choose', default=None, required=True) 41 | 42 | args = parser.parse_args() 43 | try: 44 | metric_path = metrics[args.metric] 45 | except KeyError: 46 | print "Error: Unrecognized/unsupported metric '{_metric}'".format(_metric=args.metric) 47 | str_metrics = "Supported Metrics: [" 48 | for metric in metrics: 49 | str_metrics += ("'" + metric + "',") 50 | print str_metrics[:-1] + "]" 51 | sys.exit(status_code) 52 | if (args.critical and args.critical < 0): 53 | print "Error: -C Critical cannot be negative" 54 | sys.exit(status_code) 55 | elif (args.warning and args.warning < 0): 56 | print "Error: -W Warning cannot be negative" 57 | sys.exit(status_code) 58 | 59 | #default url = http://127.0.0.1:5984/_stats 60 | url = "http://{host}:{port}/_stats/{_path1}/{_path2}".format(host=args.host,port=args.port, 61 | _path1=metric_path[0], _path2=metric_path[1]) 62 | 63 | couchdb_obj = json.loads(urllib2.urlopen(url).read()) 64 | metric_value = couchdb_obj[metric_path[0]][metric_path[1]][metric_path[2]] 65 | if (metric_value is None): 66 | metric_value = 0 67 | 68 | if (args.critical and metric_value >= args.critical): 69 | print "CRITICAL - " + metric_path[3] + ": " + str(metric_value) + "|" + args.metric + "=" + str(metric_value) 70 | status_code = 2 71 | elif (args.warning and metric_value >= args.warning): 72 | print "WARNING - " + metric_path[3] + ": " + str(metric_value) + "|" + args.metric + "=" + str(metric_value) 73 | status_code = 1 74 | else: 75 | print "OK - " + metric_path[3] + ": " + str(metric_value) + "|" + args.metric + "=" + str(metric_value) 76 | status_code = 0 77 | 78 | sys.exit(status_code) 79 | -------------------------------------------------------------------------------- /check_couchbase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Author: Tony Ling 5 | Reads and parses Couchbase cbstats tool. This script work as a wrapper around 'cbstats all'. 6 | Metrics the user can choose from can be any of the keys turned from cbstats 'all' option. 7 | All warnings are triggered if metric values are greater than thresholds 8 | except for uptime and total requests which have no warning thresholds. 9 | For a list of all metrics, run '/opt/couchbase/bin/cbstats [host]:[port] all' in terminal. 10 | 11 | Example Usage: 12 | python check_couchbase.py -t curr_connections -W 100 -C 150 13 | 14 | Created on: 12/05/14 15 | """ 16 | import sys 17 | import argparse 18 | import subprocess 19 | 20 | parser = argparse.ArgumentParser() 21 | parser.add_argument('-H', '--host', help='Host location, default=localhost', default='localhost') 22 | parser.add_argument('-P', '--port', help='Port number, default=11210', default='11210') 23 | parser.add_argument('-W', '--warning', help='Warning threshold', default=None, type=float) 24 | parser.add_argument('-C', '--critical', help='Critical theshold', default=None, type=float) 25 | parser.add_argument('-b', '--bucket', help='Bucket name, default=\'default\'', default='default') 26 | parser.add_argument('-p', '--password', help='Bucket password if required', default=None) 27 | parser.add_argument('-t', '--metric', help='Metric to choose from \'cbstats all\' output', default=None, required=True) 28 | args = parser.parse_args() 29 | 30 | def print_status(status, msg, value, metric): 31 | if status is 2: 32 | print "CRITICAL - " + msg + ": " + str(value) + "|" + metric + "=" + str(value) 33 | elif status is 1: 34 | print "WARNING - " + msg + ": " + str(value) + "|" + metric + "=" + str(value) 35 | else: 36 | print "OK - " + msg + ": " + str(value) + "|" + metric + "=" + str(value) 37 | 38 | def process_output(output): 39 | status_code = 3 40 | g = subprocess.Popen(["grep", "{m}".format(m=args.metric)], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 41 | out = g.communicate(input=output)[0].strip().split('\n') 42 | found = False 43 | if len(out) > 1: 44 | for ele in out: 45 | if (ele.split(":")[0].strip() == args.metric): 46 | found = True 47 | value = ele.split(":")[1].strip() 48 | else: 49 | if out[0] is "": 50 | pass 51 | else: 52 | found = True 53 | value = out[0].split()[1] 54 | if not found: 55 | print "Error {m} metric not found in 'cbstats all'".format(m=args.metric) 56 | else: 57 | try: 58 | if (args.critical and int(value) >= args.critical): 59 | status_code = 2 60 | print_status(status_code, args.metric, value, args.metric) 61 | elif (args.warning and int(value) >= args.warning): 62 | status_code = 1 63 | print_status(status_code, args.metric, value, args.metric) 64 | else: 65 | status_code = 0 66 | print_status(status_code, args.metric, value, args.metric) 67 | except: # catches attempts at trying to compare a string with a threshold value 68 | status_code = 0 69 | print_status(status_code, args.metric, value, args.metric) 70 | return status_code 71 | 72 | # code starts here 73 | status_code = 3 74 | if (args.critical and args.critical < 0): 75 | print "Error: -C Critical cannot be negative" 76 | elif (args.warning and args.warning < 0): 77 | print "Error: -W Warning cannot be negative" 78 | else: 79 | if (args.password): 80 | p = subprocess.Popen(["/opt/couchbase/bin/cbstats", "{host}:{port}".format(host=args.host, port=args.port), "-b", "{bucket}".format(bucket=args.bucket), "-p", "{passw}".format(passw=args.password), "all"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 81 | out, err = p.communicate() 82 | status_code = sysprocess_output(out) 83 | else: 84 | p = subprocess.Popen(["/opt/couchbase/bin/cbstats", "{host}:{port}".format(host=args.host, port=args.port), "-b", "{bucket}".format(bucket=args.bucket), "all"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 85 | out, err = p.communicate() 86 | status_code = process_output(out) 87 | sys.exit(status_code) 88 | -------------------------------------------------------------------------------- /af.oracle.metrics/src/main/java/com/os/appfirst/MysqlTestsQueries.java: -------------------------------------------------------------------------------- 1 | package com.os.appfirst; 2 | 3 | 4 | import java.sql.Connection; 5 | import java.sql.DriverManager; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.sql.Statement; 9 | import java.util.concurrent.atomic.AtomicLong; 10 | 11 | 12 | import javax.naming.Context; 13 | import javax.naming.InitialContext; 14 | import javax.naming.NamingException; 15 | import javax.sql.DataSource; 16 | 17 | import org.apache.log4j.Logger; 18 | 19 | 20 | public class MysqlTestsQueries { 21 | 22 | static final Logger logger = Logger.getLogger(MysqlTestsQueries.class); 23 | private final AtomicLong counter = new AtomicLong(); 24 | 25 | public MysqlTestsQueries() { 26 | // TODO Auto-generated constructor stub 27 | } 28 | 29 | public void doTest() { 30 | 31 | Context ctx = null; 32 | Connection con = null; 33 | Statement stmt = null; 34 | ResultSet rs = null; 35 | try{ 36 | ctx = new InitialContext(); 37 | DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MyLocalDB"); 38 | 39 | con = ds.getConnection(); 40 | stmt = con.createStatement(); 41 | if (stmt != null){ 42 | rs = stmt.executeQuery("SELECT * FROM users;"); 43 | } else { 44 | logger.fatal("createStatement error"); 45 | } 46 | 47 | 48 | 49 | // while(rs.next()){ 50 | // 51 | // logger.debug(rs.getInt("id")); 52 | // logger.debug(rs.getString("name")); 53 | // logger.debug("---"); 54 | // } 55 | 56 | 57 | }catch(NamingException e){ 58 | e.printStackTrace(); 59 | } catch (SQLException e) { 60 | e.printStackTrace(); 61 | }finally{ 62 | try { 63 | rs.close(); 64 | stmt.close(); 65 | con.close(); 66 | ctx.close(); 67 | } catch (SQLException e) { 68 | System.out.println("Exception in closing DB resources"); 69 | } catch (NamingException e) { 70 | System.out.println("Exception in closing Context"); 71 | } 72 | 73 | } 74 | } 75 | 76 | 77 | public void doTest1() { 78 | String dbURL = "jdbc:mysql://localhost:3306/af_tests"; 79 | String username ="root"; 80 | String password = "7012296"; 81 | 82 | Connection dbCon = null; 83 | Statement stmt = null; 84 | ResultSet rs; 85 | long i = this.counter.incrementAndGet(); 86 | //String query ="INSERT INTO `users` (`name`) VALUES ('name-" + i + "');"; 87 | String query ="SELECT * FROM users;"; 88 | 89 | try { 90 | Class.forName("com.mysql.jdbc.Driver"); 91 | 92 | //getting database connection to MySQL server 93 | dbCon = DriverManager.getConnection(dbURL, username, password); 94 | 95 | //getting PreparedStatment to execute query 96 | stmt = dbCon.prepareStatement(query); 97 | 98 | //Resultset returned by query 99 | // rs = stmt.executeUpdate(query); 100 | rs = stmt.executeQuery(query); 101 | 102 | dbCon.close(); 103 | 104 | } catch (SQLException ex) { 105 | logger.debug(ex); 106 | } catch (ClassNotFoundException e) { 107 | // TODO Auto-generated catch block 108 | e.printStackTrace(); 109 | } finally{ 110 | if (dbCon != null){ 111 | try { 112 | dbCon.close(); 113 | } catch (SQLException e) { 114 | // TODO Auto-generated catch block 115 | e.printStackTrace(); 116 | } 117 | } 118 | //close connection ,stmt and resultset here 119 | } 120 | 121 | } 122 | 123 | } 124 | 125 | -------------------------------------------------------------------------------- /check_haproxy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Author: Tony Ling 5 | Checks haproxy stats page and parses the output depending on the metric. 6 | The script adds up the total metric values of all servers. 7 | For use with AppFirst collector 8 | 9 | Example Usage: 10 | python /usr/share/appfirst/plugins/libexec/check_haproxy.py -P 1935 -L haproxy?stats -U guest -p guest -t qcur -W 500 -C 1000 11 | 12 | Created on: 8/22/14 13 | """ 14 | 15 | import sys 16 | import argparse 17 | import urllib2 18 | import csv 19 | import base64 20 | import StringIO 21 | 22 | status_code = 3; 23 | 24 | #metrics dict key is the cvs column metric and value is output string 25 | metrics = {} 26 | metrics['econ'] = 'Total connection errors' 27 | metrics['ereq'] = 'Total error requests' 28 | metrics['eresp'] = 'Total error responses' 29 | metrics['qcur'] = 'Total current queues' 30 | metrics['scur'] = 'Total current sessions' 31 | metrics['stot'] = 'Total sessions' 32 | 33 | parser = argparse.ArgumentParser() 34 | parser.add_argument('-H', '--host', help='Host location, default=localhost', default='localhost') 35 | parser.add_argument('-P', '--port', help='Port number', default=None, type=int) 36 | parser.add_argument('-U', '--username', help='Username for authentication, default=guest', default='guest') 37 | parser.add_argument('-p', '--password', help='Password for authentication, default=guest', default='guest') 38 | parser.add_argument('-L', '--location', help='Path location of stats page, ex: [host]:[port]/haproxy?stats', default=None) 39 | parser.add_argument('-W', '--warning', help='Warning threshold', default=None, type=float) 40 | parser.add_argument('-C', '--critical', help='Critical theshold', default=None, type=float) 41 | parser.add_argument('-t', '--metric', help='Metric to choose', default=None, required=True) 42 | 43 | args = parser.parse_args() 44 | try: 45 | msg = metrics[args.metric] 46 | metric = args.metric 47 | except KeyError: 48 | print "Error: Unrecognized/unsupported metric '{_metric}'".format(_metric=args.metric) 49 | str_metrics = "Supported Metrics: [" 50 | for metric in metrics: 51 | str_metrics += ("'" + metric + "',") 52 | print str_metrics[:-1] + "]" 53 | sys.exit(status_code) 54 | 55 | if (args.critical and args.critical < 0): 56 | print "Error: -C Critical cannot be negative" 57 | sys.exit(status_code) 58 | elif (args.warning and args.warning < 0): 59 | print "Error: -W Warning cannot be negative" 60 | sys.exit(status_code) 61 | 62 | #example url = "http://localhost:1935/haproxy?stats;csv" 63 | if (args.port): 64 | if (args.location): 65 | request = urllib2.Request("http://{host}:{port}/{path};csv".format(host=args.host,port=args.port,path=args.location)) 66 | else: 67 | print "Error: No path location supplied. Type -h or --help for help" 68 | sys.exit(2) 69 | else: 70 | print "Error: No port supplied. Type -h or --help for help" 71 | sys.exit(2) 72 | 73 | if (args.username and args.password): 74 | base64string = base64.encodestring('%s:%s' % (args.username, args.password)).replace('\n', '') 75 | request.add_header("Authorization", "Basic %s" % base64string) 76 | 77 | csvstring = StringIO.StringIO(urllib2.urlopen(request).read()) 78 | reader = csv.reader(csvstring, delimiter=',') 79 | 80 | data = [] 81 | for row in reader: 82 | data.append(row) 83 | limit = len(data[0]) 84 | 85 | for i in range(0,limit): 86 | if (data[0][i] == metric): 87 | index = i; 88 | break; 89 | metric_value = 0 90 | for i in range(1,len(data)): 91 | if (data[i][index] is not ''): 92 | try: 93 | metric_value += int(data[i][index]) 94 | except: 95 | pass 96 | 97 | if (args.critical and metric_value >= args.critical): 98 | print "CRITICAL - " + msg + ": " + str(metric_value) + "|" + metric + "=" + str(metric_value) 99 | status_code = 2 100 | elif (args.warning and metric_value >= args.warning): 101 | print "WARNING - " + msg + ": " + str(metric_value) + "|" + metric + "=" + str(metric_value) 102 | status_code = 1 103 | else: 104 | print "OK - " + msg + ": " + str(metric_value) + "|" + metric + "=" + str(metric_value) 105 | status_code = 0 106 | 107 | sys.exit(status_code) 108 | -------------------------------------------------------------------------------- /check_resque.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | ''' 3 | Created on May 31, 2012 4 | 5 | @author: Yangming 6 | ''' 7 | import nagios 8 | from nagios import CommandBasedPlugin as plugin 9 | import commands 10 | import statsd 11 | import argparse 12 | 13 | class ResqueChecker(nagios.BatchStatusPlugin): 14 | def __init__(self, *args, **kwargs): 15 | super(ResqueChecker, self).__init__(*args, **kwargs) 16 | # Hack to determine uniqueness of script defs 17 | check = argparse.ArgumentParser() 18 | check.add_argument("-H", "--host", required=False, type=str) 19 | check.add_argument("-p", "--port", required=False, type=int) 20 | chk, unknown = check.parse_known_args() 21 | 22 | self.parser.add_argument("-f", "--filename", required=False, type=str, default='pd@resque_redis-cli') 23 | self.parser.add_argument("-u", "--user", required=False, type=str) 24 | self.parser.add_argument("-s", "--password", required=False, type=str) 25 | self.parser.add_argument("-H", "--host", required=False, type=str) 26 | self.parser.add_argument("-p", "--port", required=False, type=int) 27 | self.parser.add_argument("-n", "--database", required=False, type=int) 28 | self.parser.add_argument("-z", "--appname", required=False, type=str, default='resque') 29 | self.parser.add_argument("--unique", required=False, type=str, default=str(chk.host)+str(chk.port)) 30 | 31 | @plugin.command("QUEUE_LENGTH") 32 | @statsd.gauge 33 | def get_queue_length(self, request): 34 | stats = {} 35 | query = "smembers resque:queues" 36 | output = self.run_query(request, query) 37 | 38 | total = 0 39 | status_code = nagios.Status.OK 40 | query_pattern = "llen resque:queue:%s" 41 | for q in output.split('\n'): 42 | if q: 43 | v = self.run_query(request, query_pattern % q) 44 | stats[q] = int(v) 45 | total += stats[q] 46 | sc = self.verdict(v, request.warn, request.crit) 47 | if sc == nagios.Status.WARNING and status_code == nagios.Status.OK: 48 | status_code = nagios.Status.WARNING 49 | elif sc == nagios.Status.CRITICAL: 50 | status_code = nagios.Status.CRITICAL 51 | 52 | r = nagios.Result(request.option, status_code, '%s jobs in queues' % total, request.appname); 53 | r.add_performance_data('total', total, warn=request.warn, crit=request.crit) 54 | for k, v in stats.iteritems(): 55 | r.add_performance_data(k, v, warn=request.warn, crit=request.crit) 56 | return r 57 | 58 | @plugin.command("JOB_PROCESSED") 59 | @statsd.counter 60 | def get_job_processed(self, request): 61 | query = "get resque:stat:processed" 62 | output = self.run_query(request, query) 63 | if output is None: 64 | delta = 0 65 | else: 66 | value = int(output) 67 | laststats = self.retrieve_last_status(request) 68 | delta = value - laststats.setdefault("processed", 0) 69 | laststats["processed"] = value 70 | self.save_status(request, laststats) 71 | 72 | return self.get_result(request, value, '%s jobs in processed' % delta) 73 | 74 | def run_query(self, request, query): 75 | cmd_template = "redis-cli --raw" 76 | if request.password is not None: 77 | cmd_template += " -a %s" % request.password 78 | if request.database is not None: 79 | cmd_template += " -n %s" % request.database 80 | if request.host is not None: 81 | cmd_template += " -h %s" % request.host 82 | if request.port is not None: 83 | cmd_template += " -p %s" % request.port 84 | cmd = "%s %s" % (cmd_template, query) 85 | if request.user: 86 | cmd = nagios.rootify(cmd, request.user) 87 | output = commands.getoutput(cmd) 88 | if "command not found" in output: 89 | raise nagios.ServiceInaccessibleError(request, output) 90 | elif output.strip() == "": 91 | raise nagios.ServiceInaccessibleError(request, output) 92 | return output 93 | 94 | if __name__ == "__main__": 95 | import sys 96 | ResqueChecker().run(sys.argv[1:]) 97 | -------------------------------------------------------------------------------- /jmx-collector/jmxcollector: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # /etc/init.d/jmxcollector 3 | # 4 | # on Ubuntu requires packages: 5 | # openjdk-7-jre AND openjdk-7-jdk 6 | # 7 | # chkconfig: 2345 51 31 8 | # description: Start/stop/restart jmxcollector 9 | # 10 | BASEDIR=/usr/share/appfirst 11 | PRODIR=$BASEDIR/plugins/libexec/jmx-collector 12 | CONFDIR=$PRODIR/../../etc 13 | CONFILE=nrpe_appfirst.cfg 14 | DAEMON=com.objectstyle.appfirst.jmx.collector.Application 15 | 16 | # If you want to connect to a JVM that is not running as root 17 | # change this variable to the user name as needed. 18 | USER=root 19 | 20 | # NOTE: 21 | # You must have JAVA_HOME set. Prios as follows: 22 | # 1. hardcoded JAVA_HOME variable in this script 23 | # overrides everything 24 | # 25 | # 2. JAVA_HOME defined in the config file overides any 26 | # environment variable. The form of a config element in the 27 | # config file is: env_command[JAVA_HOME]=pathname 28 | # 29 | # 3. An environment variable set using 30 | # % export JAVA_HOME=pathname 31 | # 32 | # 4. If nothing is set we try /usr/java/latest 33 | # This may not work, just taking a shot 34 | # if nothing else is available 35 | # 36 | 37 | #JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/ 38 | #JAVA_HOME=/usr/java/latest/ 39 | 40 | if [ -e /etc/rc.d/init.d/functions ] 41 | then 42 | . /etc/rc.d/init.d/functions 43 | failmsg=failure 44 | warnmsg=warning 45 | successmsg=success 46 | infomsg=echo 47 | status=status 48 | else 49 | if [ -e /lib/lsb/init-functions ] 50 | then 51 | . /lib/lsb/init-functions 52 | failmsg=log_failure_msg 53 | warnmsg=log_warning_msg 54 | successmsg=log_success_msg 55 | infomsg=echo 56 | status=/sbin/pidof 57 | fi 58 | fi 59 | 60 | # The rc interface: 61 | case "$1" in 62 | start) 63 | if pgrep -f $DAEMON >> /dev/null 64 | then 65 | $failmsg "jmxcollector is already running" 66 | exit 0 67 | fi 68 | 69 | if grep jmx_command $CONFDIR/$CONFILE | grep -v "#" >> /dev/null 70 | then 71 | if [ ! $JAVA_HOME ] 72 | then 73 | if grep env_command $CONFDIR/$CONFILE | grep -v "#" | grep JAVA_HOME >> /dev/null 74 | then 75 | JAVA_HOME=`grep env_command $CONFDIR/$CONFILE | grep -v "#" | grep JAVA_HOME | cut -d \= -f 2` 76 | fi 77 | fi 78 | 79 | if grep env_command $CONFDIR/$CONFILE | grep -v "#" | grep JAVA_USER >> /dev/null 80 | then 81 | USER=`grep env_command $CONFDIR/$CONFILE | grep -v "#" | grep JAVA_USER | cut -d \= -f 2` 82 | fi 83 | 84 | if [ ! $JAVA_HOME ] 85 | then 86 | $infomsg 'You do not have a JAVA_HOME environment variable' 87 | $infomsg 'We will try using JAVA_HOME=/usr/java/latest. It may or may not work for you. Please verify' 88 | JAVA_HOME='/usr/java/latest' 89 | fi 90 | 91 | $infomsg "Starting jmxcollector using JAVA_HOME=$JAVA_HOME and USER=$USER" 92 | 93 | cd $PRODIR 94 | su -s /bin/sh $USER -c "LD_LIBRARY_PATH=$BASEDIR ${JAVA_HOME}/bin/java -cp .:${JAVA_HOME}/lib/jconsole.jar:${JAVA_HOME}/lib/tools.jar:appfirst-jmx-0.5-jar-with-dependencies.jar:appfirst-jmx-0.5.jar com.objectstyle.appfirst.jmx.collector.Application >/dev/null 2>>/dev/null &" 95 | 96 | RETVAL=$? 97 | 98 | sleep 1 99 | if pgrep -f $DAEMON >> /dev/null 100 | then 101 | $successmsg "jmxcollector has started" 102 | else 103 | $failmsg "jmxcollector failed to start" 104 | fi 105 | fi 106 | ;; 107 | stop) 108 | if pgrep -f $DAEMON >> /dev/null 109 | then 110 | $infomsg "trying to stop $DAEMON" 111 | pkill -9 -f $DAEMON 112 | RETVAL=$? 113 | while pgrep -f $DAEMON >> /dev/null 114 | do 115 | sleep 1 116 | done 117 | $successmsg "jmxcollector has stopped" 118 | fi 119 | ;; 120 | status) 121 | if pgrep -f $DAEMON >> /dev/null 122 | then 123 | $successmsg "jmxcollector is running" 124 | else 125 | $successmsg "jmxcollector is stopped" 126 | fi 127 | ;; 128 | restart) 129 | $0 stop 130 | $0 start $2 131 | ;; 132 | *) 133 | $failmsg "Usage: /etc/init.d/afalert {start|stop|restart|status}" 134 | exit 1 135 | ;; 136 | esac 137 | 138 | exit $RETVAL 139 | -------------------------------------------------------------------------------- /jmx-collector/src/main/java/com/objectstyle/appfirst/jmx/collector/config/CommandBuilder.java: -------------------------------------------------------------------------------- 1 | package com.objectstyle.appfirst.jmx.collector.config; 2 | 3 | import com.objectstyle.appfirst.jmx.collector.command.*; 4 | 5 | import javax.management.remote.JMXServiceURL; 6 | import java.net.MalformedURLException; 7 | 8 | public class CommandBuilder implements CommandLineProcessor { 9 | private final ThresholdBuilder thresholdBuilder = new ThresholdBuilder(); 10 | 11 | private String commandName; 12 | 13 | private String mBeanName; 14 | 15 | private String mBeanAttribute; 16 | 17 | private String mBeanAttributeKey; 18 | 19 | private String virtualMachineMatchPattern; 20 | 21 | private String virtualMachineURL; 22 | 23 | private String warningThreshold; 24 | 25 | private String criticalThreshold; 26 | 27 | private boolean built; 28 | 29 | @Override 30 | public void startCommand(String commandName) { 31 | this.commandName = commandName; 32 | mBeanName = null; 33 | mBeanAttribute = null; 34 | mBeanAttributeKey = null; 35 | virtualMachineMatchPattern = null; 36 | virtualMachineURL = null; 37 | warningThreshold = null; 38 | criticalThreshold = null; 39 | built = false; 40 | } 41 | 42 | @Override 43 | public void endCommand() { 44 | built = true; 45 | } 46 | 47 | @Override 48 | public void startVirtualMachineMatchPattern(String virtualMachineMatchPattern) { 49 | this.virtualMachineMatchPattern = virtualMachineMatchPattern; 50 | } 51 | 52 | @Override 53 | public void startVirtualMachineURL(String virtualMachineURL) { 54 | this.virtualMachineURL = virtualMachineURL; 55 | } 56 | 57 | @Override 58 | public void startMBeanName(String mBeanName) { 59 | this.mBeanName = mBeanName; 60 | } 61 | 62 | @Override 63 | public void startMBeanAttribute(String mBeanAttribute) { 64 | this.mBeanAttribute = mBeanAttribute; 65 | } 66 | 67 | @Override 68 | public void startMBeanAttributeKey(String mBeanAttributeKey) { 69 | this.mBeanAttributeKey = mBeanAttributeKey; 70 | } 71 | 72 | @Override 73 | public void startWarningThreshold(String value) { 74 | this.warningThreshold = value; 75 | } 76 | 77 | @Override 78 | public void startCriticalThreshold(String value) { 79 | this.criticalThreshold = value; 80 | } 81 | 82 | public Command buildCommand() throws CommandValidationException { 83 | if (!built) { 84 | throw new IllegalStateException("Command has not been built yet"); 85 | } 86 | VirtualMachineDefinition virtualMachineDefinition; 87 | if (virtualMachineMatchPattern != null) { 88 | virtualMachineDefinition = new PatternVirtualMachineDefinition(virtualMachineMatchPattern); 89 | } else if (virtualMachineURL != null) { 90 | try { 91 | virtualMachineDefinition = new URLVirtualMachineDefinition(new JMXServiceURL(virtualMachineURL)); 92 | } catch (MalformedURLException e) { 93 | throw new CommandValidationException("Malformed URL", e); 94 | } 95 | } else { 96 | throw new CommandValidationException("Virtual machine pattern or URL are not provided"); 97 | } 98 | ValueDefinition valueDefinition; 99 | if (mBeanName == null) { 100 | throw new CommandValidationException("Object name is not provided"); 101 | } 102 | if (mBeanAttribute == null) { 103 | throw new CommandValidationException("Object attribute name is not provided"); 104 | } 105 | if (mBeanAttributeKey != null) { 106 | valueDefinition = new CompositeValueDefinition(mBeanName, mBeanAttribute, mBeanAttributeKey); 107 | } else { 108 | valueDefinition = new ValueDefinition(mBeanName, mBeanAttribute); 109 | } 110 | ThresholdDefinition warningThresholdDefinition = thresholdBuilder.fromString(warningThreshold); 111 | ThresholdDefinition criticalThresholdDefinition = thresholdBuilder.fromString(criticalThreshold); 112 | return new Command(commandName, virtualMachineDefinition, valueDefinition, 113 | warningThresholdDefinition, criticalThresholdDefinition); 114 | } 115 | } 116 | --------------------------------------------------------------------------------