Spring bootのアプリケーションによるアプリとかシステムとかの情報を持ってくる例

はい、Spring bootでのサンプルです。
そして、仕事の技術調査の結果です

アプリケーションを実行していて、この情報を引っ張ってくる方法って?
と思ったので、その方法を調査ついでにサンプルソースを作成しました。

https://github.com/wataru775/sample-spring-information

解説!って程度も要らないかもしれないですが、解説!

Javaのバージョン!
はい、シンプルですね。
環境変数に入っていますので、それを取ります

    private String getJavaVersion(){
        return System.getProperty("java.version");
    }

端末名

    private String getHostName(){
        try {
            return InetAddress.getLocalHost().getHostName() ;
        }catch(UnknownHostException e){

        }
        return "UNKNOW HOST NAME";
    }

取れれば、その名前、取れなかったらUNKNOWです。

IPアドレス
これはめんどくさかった…

    private String getIpAddress(){
        try {
            return( getInetAddress4().getHostAddress() );
        }catch(SocketException e){

        }catch(UnknownHostException e){

        }
        return "UNKNOWN IP ADDRESS";
    }
    private InetAddress getInetAddress4() throws UnknownHostException,SocketException {
        InetAddress rtnInet = null;
        Enumeration  netSet;//集合内の列挙操作用
        netSet = NetworkInterface.getNetworkInterfaces();
        while(netSet.hasMoreElements()){//すべてのインターフェイスを走査
            NetworkInterface nInterface = (NetworkInterface) netSet.nextElement();
            List list = nInterface.getInterfaceAddresses();
            if( list.size() == 0 ) continue;
            for (InterfaceAddress interfaceAdr : list){
                InetAddress inet = interfaceAdr.getAddress();
                if(inet.isLoopbackAddress() ) continue;
                if(inet.getClass() == Inet4Address.class) {
                    rtnInet = inet;
                }
            }
        }
        return rtnInet;
    }

ですね…って私のソースじゃないですね。
getInetAddress4()の書き方が違います。
教えて!Google先生!
http://manabu.quu.cc/up/6/e62090.htmを参考…ってかコピペですね><
ダメじゃん、複数インタフェイスあっても、一つしか認識していないしw
要検討と言うことで次

MACアドレス

    private String getMACAddress(){

        Enumeration nics;
        try {
            nics = NetworkInterface.getNetworkInterfaces();
            while(nics.hasMoreElements()){
                NetworkInterface nic = nics.nextElement();
                if(!(nic.getDisplayName().equals("eth0") || nic.getDisplayName().equals("en0")))continue;
                String macAddress = "";
                byte[] hardwareAddress = nic.getHardwareAddress();
                if(hardwareAddress != null){
                    for(byte b : hardwareAddress){
                        macAddress += String.format("%02X ", b);
                    }
                }
                return  macAddress;
            }
        } catch (SocketException e) {
        }

        return "UNKNOWN MAC ADDRESS";

    }

むむむっ!これもだな、Enumerationとwhileと「macAddress += 」が私流ではない。
eth0 / en0の変態っぽい書き方は私の調査用の書き方ですが
ま、いっか!

次が説明が大変系ですな。
参考にしたサイトはこちら : http://blog.y-yuki.net/entry/2016/11/27/000000
ほぼこちらに違いですな。

始めに、application.propertiesにバージョンを書き出すっと(サイトではymlですが)

info.application.name=@project.name@
info.application.version=@project.version@

次に割当用のクラスを作るっと

package org.mmpp.sample.infomation;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@Data
@ConfigurationProperties(prefix="info.application")
public class ApplicationContext {
        private String name;
        private String version;
}

んで、使うところで紐付けするっと

    @Autowired
    private ApplicationContext applicationContext;

ほんでもって使うっと

    private String getAppicationVersion(){
        return applicationContext.getVersion();
    }

まとめてしまうとシンプルですな。

あと仕事的に必要な情報は
・サービスの動作確認 /etc/init.d/ssh statusの結果とか。execだとちょっとダサいかも
程度かな。

なんとも、サンプルをぐるぐる作ってますわ。
ちなみにBlog記事にしていないのは…
sample_tomcat_war : spring bootでtomcatのwar作る
sample_application: spring bootのコマンドアプリの作り方
sample_pi4j_boot : pi4jによるLチカサンプル
sample_services : Webサービス… ゴミだね。
sample_atom_client: rss atomのクライアント
sample_spring_mvc_login:spring securityによるログイン制限
sample_web_atom : rss atomの配信サーバ

多いなw
正直今となってはゴミに近い部分もありますが…
そのほかに、技術Fixでアプリケーションもあるので、結構なヴォリュームが残っておりますな!

近々に整理しながら上げていきます