イニシャライザー

class User {
	private String name;
	private static int count;

	static {
		User.count = 0;
		System.out.println("Static initializer");
	}

	public User(String name){
		this.name = name;
		User.count++;
		System.out.println("Constructor initializer");
	}

	{
		System.out.println("Instance initializer");
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

public class MyApp {

	public static void main(String[] args){
		User.getInfo();
		User tom = new User("Tom");
		User.getInfo();
		User john = new User("John");
		User.getInfo();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Static initializer
# of instances: 0
Instance initializer
Constructor initializer
# of instances: 1
Instance initializer
Constructor initializer
# of instances: 2

final は継承できない。

final class User {
	protected String name;
	private static final double VERSION = 1.1;

	static {
		this.name = name;
		User.VERSION = 1.2;
	}

	public final void sayHi{
		System.out.println("Hi! " + this.name);
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

抽象クラス

// 抽象クラス -> 具象クラス

abstract class User {
	public abstract void sayHi(); // 抽象メソッド
}

class JapaneseUser extends User {
	@Override
	public void sayHi(){
		System.out.println("こんにちは!");
	}
}

class AmericanUser extends User {
	@Override
	public void sayHi(){
		System.out.println("Hi");
	}
}

public class MyApp {

	public static void main(String[] args){
		AmericanUser tom = new AmericanUser();
		JapaneseUser aki = new JapaneseUser();
		tom.sayHi();
		aki.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi
こんにちは!

interface

interface Printable{
	// 定数
	double VERSION = 1.2;
	// 抽象メソッド
	void print();
	// default メソッド
	public default void getInfo(){
		System.out.println("I/F ver. " + Printable.VERSION);
	} 
	// static メソッド

} 

class User implements Printable {
	@Override
	public void print(){
		System.out.println("Now printing user profile...");
	}
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User();
		tom.print();
		tom.getInfo();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Now printing user profile…
I/F ver. 1.2

列挙型

enum Result {
	SUCCESS,
	ERROR,

}


public class MyApp {

	public static void main(String[] args){
		Result res;

		res = Result.ERROR;

		switch (res){
			case SUCCESS:
				System.out.println("OK!");
				System.out.println(res.ordinal());
			break;
			case ERROR:
				System.out.println("NG!");
				System.out.println(res.ordinal());
				break;
		}

		
	}
}

[vagrant@localhost java]$ java MyApp
NG!
1

例外処理

public class MyApp {

	public static void div(int a, int b){
		System.out.println(a / b);
	}

	public static void main(String[] args){
		div(3, 0);
	}
}

[vagrant@localhost java]$ java MyApp
Exception in thread “main” java.lang.ArithmeticException: / by zero
at MyApp.div(MyApp.java:13)
at MyApp.main(MyApp.java:17)

例外処理

class MyException extends Exception {
	public MyException(String s){
		super(s);
	}
}

public class MyApp {

	public static void div(int a, int b){

		try {
			if (b < 0){
				throw new MyException("not minux!");
			}
			System.out.println(a / b);
		} catch (ArithmeticException e){
			System.err.println(e.getMessage());
		} catch(MyException e){
			System.err.println(e.getMessage());
		}		
	}
	public static void main(String&#91;&#93; args){
		div(3, 0);
		div(5, -2);
	}
}
&#91;/java&#93;

Wrapper Class
&#91;java&#93;
public class MyApp {

	public static void main(String&#91;&#93; args){
		// Integer i = new Integer(32);
		// int n = i.intValue();
		Integer i = 32; // auto boxing
		i = null;
		int n = i; // auto unboxing
		System.out.println();
	}
}
&#91;/java&#93;

&#91;vagrant@localhost java&#93;$ javac MyApp.java
&#91;vagrant@localhost java&#93;$ java MyApp
Exception in thread "main" java.lang.NullPointerException
        at MyApp.main(MyApp.java:14)

型を後程指定するジェネリック
&#91;java&#93;
class MyData<T> {
	public void getThree(T x){
		System.out.println(x);
		System.out.println(x);
		System.out.println(x);
	}
}

public class MyApp {

	public static void main(String[] args){
		MyData<Integer> i = new MyData<>();
		i.getThree(32);
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
32
32
32

パッケージをコンパイルしよう

インポートって、要するに違うファイルからの読み込みね。すっかり忘れてる or スルーしてた。

package jp.hpscript.myapp;
import jp.hpscript.myapp.model.User;
import jp.hpscript.myapp.model.AdminUser;

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		// System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		// System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}
package jp.hpscript.myapp.model;

public class User {
	protected String name;

	public User(String name){
		this.name = name;
	}

	public void sayHi(){
		System.out.println("hi!" + this.name);
	}
}
package jp.hpscript.myapp.model;

public class AdminUser extends User {
	public AdminUser(String name){
		super(name);
	}

	public void sayHello(){
		System.out.println("hello!" + this.name);
	}
	@Override public void sayHi(){
		System.out.println("[admin]hi!" + this.name);
	}	
}

[vagrant@localhost java]$ javac jp/hpscript/myapp/MyApp.java
[vagrant@localhost java]$ java jp.hpscript.myapp.MyApp
hi!Tom
[admin]hi!bob
hello!bob

setter, getter

class User {
	private String name;
	private int score;

	public User(String name, int score){
		this.name = name;
		this.score = score;
	}

	public int getScore(){
		return this.score;
	}

	public void setScore(int score){
		if(score > 0){
			this.score = score;
		}	
	}
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom", 65);
		tom.setScore(85);
		tom.setScore(-22);
		System.out.println(tom.getScore());
	}
}
// static

class User {
	private String name;
	private static int count = 0;

	public User(String name){
		this.name = name;
		User.count++;
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

public class MyApp {

	public static void main(String[] args){
		User.getInfo();
		User tom = new User("Tom");
		User.getInfo();
		User john = new User("John");
		User.getInfo();
	}
}

java method

public static void sayHi(){
		System.out.println("Hi!");
	}

	public static void main(String[] args){
		sayHi();
	}
public static void sayHi(String name){
		System.out.println("Hi!" + name);
	}

	public static void main(String[] args){
		sayHi("Tom");
		sayHi("Bob");
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi!Tom
Hi!Bob

public static String sayHi(String name){
		return "Hi!" + name;
	}

	public static void main(String[] args){
		String msg = sayHi("Steve");
		System.out.println(msg);
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi!Steve

classを使ってみよう

class User {
	String name = "Me!";

	void sayHi(){
		System.out.println("hi!");
	}

}

public class MyApp {

	public static void main(String[] args){
		User tom;
		tom = new User();

		System.out.println(tom.name);
		tom.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Me!
hi!

なるほど。

コンストラクタ

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}

}

public class MyApp {

	public static void main(String[] args){
		User tom;
		tom = new User("Tom");

		System.out.println(tom.name);
		tom.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Tom
hi!Tom

なるほど、何故か急に分かってきた。
classの継承

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}
}

class AdminUser extends User {
	AdminUser(String name){
		super(name);
	}

	void sayHello(){
		System.out.println("hello!" + this.name);
	}	
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}

@override 継承した時に親クラスのメソッドを使う

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}
}

class AdminUser extends User {
	AdminUser(String name){
		super(name);
	}

	void sayHello(){
		System.out.println("hello!" + this.name);
	}
	@Override void sayHi(){
		System.out.println("[admin]hi!" + this.name);
	}	
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Tom
hi!Tom
bob
[admin]hi!bob
hello!bob
ほうほう

java 基礎

public static void main(String[] args){
		int[] sales;
		sales = new int[3];

		sales[0] = 100;
		sales[1] = 200;
		sales[2] = 300;
		System.out.println(sales[1]);
		
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
200

こうも書ける

public static void main(String[] args){
		int[] sales;
		sales = new int[]{200, 240, 280};

		System.out.println(sales[1]);

	}

forと組み合わせる。

public static void main(String[] args){

int[] sales = {800, 540, 320};

for(int i = 0; i < 3; i++){ System.out.println(sales[i]); } } [/java] (int sale: sales)という書き方 [java] public static void main(String[] args){ int[] sales = {800, 540, 320}; for(int sale: sales){ System.out.println(sale); } } [/java] 基本データ型と参照型 [java] public static void main(String[] args){ int x = 10; int y = x; y = 5; System.out.println(x); System.out.println(y); } [/java] [java] int[] a = {3, 5, 7}; int[] b = a; b[1] = 8; System.out.println(a[1]); System.out.println(b[1]); [/java] 配列の場合はメモリ領域が入る。 [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 8 8 文字列は別の領域に割り当てる。 [java] public static void main(String[] args){ String s = "hello"; String t = s; t = "world"; System.out.println(s); System.out.println(t); } [/java]

javaをもう一回やろう

キャストする

public static void main(String[] args){
		double d = 52343.231;
		int i = (int)d;
		System.out.println(i);
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java Myapp
エラー: メイン・クラスMyappが見つからなかったかロードできませんでした
[vagrant@localhost java]$ java MyApp
52343

if文

public static void main(String[] args){
		int score = 85;
		if(score > 80){
			System.out.println("great");	
		}			
	}
public static void main(String[] args){
		int score = 85;
		String msg = score > 80 ? "great" : "soso..!";
		System.out.println(msg);
	}

switch

public static void main(String[] args){
		String signal = "red";
		switch(signal) {
			case "red":
				System.out.println("stop!");
				break;
			case "blue":
				System.out.println("go");
				break;
			case "yello":
				System.out.println("caution");
				break;
			default:
				System.out.println("wrong signal!");
				break;
		}
	}

public static void main(String[] args){
int i = 0;
while (i < 10){ System.out.println(i); i++; } } [/java] [vagrant@localhost java]$ java MyApp 0 1 2 3 4 5 6 7 8 9 こうも行ける。 [java] public static void main(String[] args){ int i = 0; do { System.out.println(i); i++; } while (i < 10); } [/java] [java] public static void main(String[] args){ for (int i = 0; i < 10; i++){ System.out.println(i); } } [/java] break, continue [java] public static void main(String[] args){ for (int i = 0; i < 10; i++){ if (i==5){ continue; } System.out.println(i); } } [/java] うおおおおおおお、短期間で詰め込んでるな

JDK、JRE/JVM

MyApp.java -> Compile -> MyApp.class

Write once, run anywhere

ではjavaの基礎から行きます。

public class MyApp {

	public static void main(String[] args){
		System.out.println("Hello world");
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ ls
AdminUser.class MyApp.class MyInteger.class User.class
AmericanUser.class MyApp.java MyRunnable.class com
JapaneseUser.class MyData.class Printable.class java1
MyApp$1.class MyException.class Result.class
[vagrant@localhost java]$ java MyApp
Hello world

変数

public class MyApp {

	public static void main(String[] args){
		// 変数
		String msg;
		msg = "hello world again";

		System.out.println(msg);
	}
}
public class MyApp {

	public static void main(String[] args){

		char a = 'a';
		// 整数 byte short int long
		int x = 10;
		long y = 55555555L;
		double d = 23423.344;
		float f = 32.33F;
		boolean flag = true; // flase
		// \n, \t

		String msg = "hello w\norld a\tgain";

		System.out.println(msg);
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
hello w
orld a gain

public static void main(String[] args){

		int i;
		i = 10 / 3;
		System.out.println(i);
		i = 10 % 3;
		System.out.println(i);
		int x = 5;
		x++;
		System.out.println(x);

	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
3
1
6

ほ~

zabbix, zabbix20, zabbix22 違い

zabbixはoss総合監視ツール ラトビアzabbixSIA社開発

linux, windows, nw
zabbix agent, snmp, ipmi, エージェントレス監視
リソース監視、死活監視(ICMPPing, プロセス)、web監視、ログ監視、HW監視、SQL監視

2.0系、2.2系、3.0系と、どんどん新しくなっている。
主に性能改善、機能拡充

監視ロジックの改善、メモリの改善、監視の幅、自動化

なるほど、当たり前だけど、バージョンが高い方がいいのね。
では入れていきましょう。
sudo yum install -y zabbix22.x86_64 zabbix22-server.noarch zabbix22-server-mysql.x86_64

エラー: パッケージ: zabbix22-server-mysql-2.2.21-1.el6.x86_64 (epel)
要求: libiksemel.so.3()(64bit)
問題を回避するために –skip-broken を用いることができません
これらを試行できます: rpm -Va –nofiles –nodigest

zabbix3にします。
yum install http://repo.zabbix.com/zabbix/3.0/rhel/6/x86_64/zabbix-release-3.0-1.el6.noarch.rpm

インストール:
zabbix-release.noarch 0:3.0-1.el6

完了しました!

yum -yの[-y]とは?

オプション -y は 問い合わせにすべて「y」と返答するオプション

$ man yum でマニュアルを見ると、

-y, –assumeyes
Assume yes; assume that the answer to any question which would
be asked is yes.
Configuration Option: assumeyes

なるほどね~

zabbixを使いたい

[vagrant@localhost dev]$ yum search zabbix
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* remi-safe: ftp.riken.jp
============================= N/S Matched: zabbix ==============================
pcp-export-zabbix-agent.x86_64 : Module for exporting from PCP into a Zabbix
: agent daemon
python-pyzabbix.noarch : PyZabbix is a Python module for working with the Zabbix
: API
uwsgi-stats-pusher-zabbix.x86_64 : uWSGI – Zabbix Stats Pusher for uWSGI
zabbix-agent.x86_64 : Zabbix Agent
zabbix-proxy.x86_64 : Zabbix Proxy
zabbix-proxy-mysql.x86_64 : Zabbix proxy compiled to use MySQL
zabbix-proxy-pgsql.x86_64 : Zabbix proxy compiled to use PostgreSQL
zabbix-proxy-sqlite3.x86_64 : Zabbix proxy compiled to use SQLite
zabbix-server.x86_64 : Zabbix server common files
zabbix-server-mysql.x86_64 : Zabbix server compiled to use MySQL
zabbix-server-pgsql.x86_64 : Zabbix server compiled to use PostgresSQL
zabbix-server-sqlite3.x86_64 : Zabbix server compiled to use SQLite
zabbix-web.noarch : Zabbix Web Frontend
zabbix-web-mysql.noarch : Zabbix web frontend for MySQL
zabbix-web-pgsql.noarch : Zabbix web frontend for PostgreSQL
zabbix-web-sqlite3.noarch : Zabbix web frontend for SQLite
zabbix20-agent.x86_64 : Zabbix agent
zabbix20-proxy.noarch : Zabbix proxy common files
zabbix20-proxy-mysql.x86_64 : Zabbix proxy compiled to use MySQL
zabbix20-proxy-pgsql.x86_64 : Zabbix proxy compiled to use PostgreSQL
zabbix20-proxy-sqlite3.x86_64 : Zabbix proxy compiled to use SQLite
zabbix20-server.noarch : Zabbix server common files
zabbix20-server-mysql.x86_64 : Zabbix server compiled to use MySQL
zabbix20-server-pgsql.x86_64 : Zabbix server compiled to use PostgresSQL
zabbix20-web.noarch : Zabbix Web Frontend
zabbix20-web-mysql.noarch : Zabbix web frontend for MySQL
zabbix20-web-pgsql.noarch : Zabbix web frontend for PostgreSQL
zabbix22-agent.x86_64 : Zabbix Agent
zabbix22-dbfiles-mysql.noarch : Zabbix database schemas, images, data and
: patches
zabbix22-dbfiles-pgsql.noarch : Zabbix database schemas, images, data and
: patches
zabbix22-dbfiles-sqlite3.noarch : Zabbix database schemas and patches
zabbix22-proxy.noarch : Zabbix Proxy
zabbix22-proxy-mysql.x86_64 : Zabbix proxy compiled to use MySQL
zabbix22-proxy-pgsql.x86_64 : Zabbix proxy compiled to use PostgreSQL
zabbix22-proxy-sqlite3.x86_64 : Zabbix proxy compiled to use SQLite
zabbix22-server.noarch : Zabbix server common files
zabbix22-server-mysql.x86_64 : Zabbix server compiled to use MySQL
zabbix22-server-pgsql.x86_64 : Zabbix server compiled to use PostgreSQL
zabbix22-web.noarch : Zabbix Web Frontend
zabbix22-web-mysql.noarch : Zabbix web frontend for MySQL
zabbix22-web-pgsql.noarch : Zabbix web frontend for PostgreSQL
zabbix.x86_64 : Open-source monitoring solution for your IT infrastructure
zabbix20.x86_64 : Open-source monitoring solution for your IT infrastructure
zabbix22.x86_64 : Open-source monitoring solution for your IT infrastructure

随分入ってますな。

Zabbixとは

Zabbixとは、サーバ、ネットワーク、アプリケーションを集中監視するための統合監視ソフトウェア

– 統合監視に必要な監視、障害検知、通知機能を備えており、多数のプラットフォームに対応したZabbixエージェントとSNMPに対応しているため、システム全体をZabbixひとつで監視することが可能
– スクリプトを作成してアプリケーションの監視を独自に拡張できたり、障害検知時にもスクリプトを実行する、Zabbixエージェントにスクリプトを実行させるなど自由に拡張を行える
– ZabbixではWebインタフェースから設定を行い、収集データはMySQLに保存する
– 監視の設定はすべてWebインタフェースから行えるため比較的設定が容易であること、データベースに保存した収集データからグラフを作成し、リソース使用状況の傾向分析や障害分析に役立てることが可能
– Zabbixは、Zabbix SIA社が開発し、オープンソースソフトウェアとして公開されている

なるほど~