├── LICENSE.txt
├── README.md
├── archive
├── dbforbix-2.0-beta-linux64.zip
├── dbforbix-2.0-beta-src.zip
├── dbforbix-2.0-beta-win.zip
├── dbforbix-2.1-beta-linux64.tar.gz
├── dbforbix-2.1-beta-src.tar.gz
├── dbforbix-2.1-beta-win.zip
├── dbforbix-2.2-beta-linux64.tar.gz
├── dbforbix-2.2-beta-src.tar.gz
├── dbforbix-2.2-beta-win.zip
├── dbforbix-2.4-linux64.tar.gz
├── dbforbix-2.4-src.tar.gz
├── dbforbix-2.4-win.zip
├── items
│ ├── db2.xml
│ ├── mssql.xml
│ ├── mysql.xml
│ ├── oracle.xml
│ ├── param.dtd
│ └── pgsql.xml
├── lib
│ └── db2java.zip
└── zbx_templates
│ ├── to_modify_template_mssql.xml
│ ├── to_modify_template_mysql.xml
│ ├── to_modify_template_ora_instance.xml
│ └── to_modify_template_postgresql.xml
├── build.xml
├── dist
├── conf
│ └── config.properties
├── dbforbix-3.0-linux64.tar.gz
├── dbforbix-3.0-src.tar.gz
├── dbforbix-3.0-win.zip
├── dbforbix.sh
├── dbforbix_run.jar
├── dbforbix_run_3.0-beta.jar
├── items
│ └── param.dtd
├── jar
│ ├── dbforbix.jar
│ └── lib
│ │ ├── commons-cli-1.3.1.jar
│ │ ├── commons-codec-1.10.jar
│ │ ├── commons-collections4-4.1.jar
│ │ ├── commons-configuration2-2.1.jar
│ │ ├── commons-daemon-1.0.15.jar
│ │ ├── commons-dbcp2-2.1.1.jar
│ │ ├── commons-lang3-3.5.jar
│ │ ├── commons-logging-1.2.jar
│ │ ├── commons-pool2-2.4.2.jar
│ │ ├── dom4j-1.6.1.jar
│ │ ├── fastjson-1.2.8.jar
│ │ ├── h2-1.4.193.jar
│ │ └── log4j-1.2.17.jar
├── linux
│ ├── dbforbix.jar
│ ├── init.d
│ │ └── dbforbix
│ ├── items
│ │ └── param.dtd
│ └── lib
│ │ ├── commons-cli-1.3.1.jar
│ │ ├── commons-codec-1.10.jar
│ │ ├── commons-collections4-4.1.jar
│ │ ├── commons-configuration2-2.1.jar
│ │ ├── commons-daemon-1.0.15.jar
│ │ ├── commons-dbcp2-2.1.1.jar
│ │ ├── commons-lang3-3.5.jar
│ │ ├── commons-logging-1.2.jar
│ │ ├── commons-pool2-2.4.2.jar
│ │ ├── dom4j-1.6.1.jar
│ │ ├── fastjson-1.2.8.jar
│ │ ├── h2-1.4.193.jar
│ │ └── log4j-1.2.17.jar
├── log4j.properties
└── win
│ ├── dbforbix.jar
│ ├── dbforbix32.exe
│ ├── dbforbix64.exe
│ ├── dbforbixw.exe
│ ├── items
│ └── param.dtd
│ └── lib
│ ├── commons-cli-1.3.1.jar
│ ├── commons-codec-1.10.jar
│ ├── commons-collections4-4.1.jar
│ ├── commons-configuration2-2.1.jar
│ ├── commons-daemon-1.0.15.jar
│ ├── commons-dbcp2-2.1.1.jar
│ ├── commons-lang3-3.5.jar
│ ├── commons-logging-1.2.jar
│ ├── commons-pool2-2.4.2.jar
│ ├── dom4j-1.6.1.jar
│ ├── fastjson-1.2.8.jar
│ ├── h2-1.4.193.jar
│ └── log4j-1.2.17.jar
├── ext_jars
├── commons-cli-1.3.1.jar
├── commons-codec-1.10.jar
├── commons-collections4-4.1.jar
├── commons-configuration2-2.1.jar
├── commons-daemon-1.0.15.jar
├── commons-dbcp2-2.1.1.jar
├── commons-lang3-3.5.jar
├── commons-logging-1.2.jar
├── commons-pool2-2.4.2.jar
├── dom4j-1.6.1.jar
├── fastjson-1.2.20.jar
├── log4j-1.2.17.jar
├── mysql-connector-java-5.1.38.jar
├── ojdbc6.jar
├── ojdbc7.jar
├── ojdbc7_g.jar
├── ojdbc8.jar
├── postgresql-9.4.1207.jre7.jar
└── sqljdbc42.jar
├── install
├── linux
│ ├── dbforbix.sh
│ └── systemd
│ │ ├── dbforbix-nojsvc.service
│ │ └── dbforbix.service
└── win
│ ├── install.cmd
│ └── uninstall.cmd
├── src
├── ObjectModel.png
├── ObjectModel.ucls
└── com
│ └── smartmarmot
│ ├── common
│ ├── Constants.java
│ ├── Persistence.java
│ ├── PersistentDB.java
│ ├── StackSingletonPersistent.java
│ └── utils
│ │ ├── DBforBIXHelper.java
│ │ ├── SAXParserDBforBIX.java
│ │ └── XMLDBforBIXUnrecoverableException.java
│ └── dbforbix
│ ├── DBforBix.java
│ ├── config
│ ├── Config.java
│ ├── Database.java
│ ├── ZabbixServer.java
│ ├── element
│ │ ├── AbstractConfigurationElement.java
│ │ ├── AbstractMultiConfigurationElement.java
│ │ ├── ConfigurationElementFactory.java
│ │ ├── DiscoveryConfigurationElement.java
│ │ ├── IConfigurationElement.java
│ │ ├── MultiColumnConfigurationElement.java
│ │ ├── MultiRowConfigurationElement.java
│ │ └── SimpleConfigurationElement.java
│ └── item
│ │ ├── ConfigurationItem.java
│ │ ├── ConfigurationItemParserFactory.java
│ │ ├── ConfigurationItemParserNative.java
│ │ ├── ConfigurationItemParserXML.java
│ │ ├── ConfigurationItemType.java
│ │ ├── IConfigurationItem.java
│ │ └── IConfigurationItemParser.java
│ ├── db
│ ├── DBConstants.java
│ ├── DBManager.java
│ ├── DBType.java
│ └── adapter
│ │ ├── ALLBASE.java
│ │ ├── AbstractDBAdapter.java
│ │ ├── DB2.java
│ │ ├── DBAdapter.java
│ │ ├── DB_NOT_DEFINED.java
│ │ ├── MSSQL.java
│ │ ├── MySQL.java
│ │ ├── Oracle.java
│ │ ├── PGSQL.java
│ │ ├── SQLANY.java
│ │ └── SYBASE.java
│ ├── scheduler
│ └── Scheduler.java
│ └── zabbix
│ ├── PersistentDBSender.java
│ ├── PersistentStackSender.java
│ ├── ZabbixItem.java
│ ├── ZabbixSender.java
│ └── protocol
│ ├── ISenderProtocol.java
│ ├── Sender14.java
│ ├── Sender18.java
│ ├── Sender32.java
│ └── json
│ ├── Data.java
│ ├── Request.java
│ └── Response.java
└── zbx_templates
├── zbx_template_DBforBIX.MySQL.xml
├── zbx_template_DBforBIX.Oracle.ASM.xml
└── zbx_template_DBforBIX.Oracle.Instances.xml
/README.md:
--------------------------------------------------------------------------------
1 | # DBforBIX
2 |
3 | DBforBix is the DB monitoring tool for Zabbix. It works like an active Zabbix Proxy by getting configuration from Zabbix Server and sending results back.
4 | Just copy dist https://github.com/smartmarmot/DBforBIX/tree/master/dist/ to your server, edit configuration and run
5 |
6 | See Wiki for DBforBix as Zabbix Proxy configuration instructions: https://github.com/smartmarmot/DBforBIX/wiki
7 |
8 | DBforBIX is licensed under the GNU General Public License V.3.
9 | You can obtain a full copy of the licese here: https://www.gnu.org/licenses/gpl-3.0.txt
10 |
11 | The project's documentation is available here: http://www.smartmarmot.com/wiki/index.php?title=DBforBIX2
12 |
13 | # Github directory structure
14 | The github repository is organized as follow:
15 | * dists: contains the packages ready to be used
16 | * items: contains all the itemfiles developed (also included in the distribution package)
17 | * template: contains all the templeates developed (also included in the distribution package)
18 | * conf: contains a sample configuration (also included in the distribution package)
19 | * src: contains all the source code
20 |
21 |
22 | ## Notes about the the structure
23 | In the dists directory you find the source code package that has generated a specific distribution. In this way you have all the code who generated a distribution.
24 | The template and items are available within the distributions files but also in a separate location to make it easy to pull change requests as well as the src and conf.
25 |
26 | ## Object Model
27 | Constructed with  (Eclipse plug-in)
28 | 
29 | 
30 |
--------------------------------------------------------------------------------
/archive/dbforbix-2.0-beta-linux64.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.0-beta-linux64.zip
--------------------------------------------------------------------------------
/archive/dbforbix-2.0-beta-src.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.0-beta-src.zip
--------------------------------------------------------------------------------
/archive/dbforbix-2.0-beta-win.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.0-beta-win.zip
--------------------------------------------------------------------------------
/archive/dbforbix-2.1-beta-linux64.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.1-beta-linux64.tar.gz
--------------------------------------------------------------------------------
/archive/dbforbix-2.1-beta-src.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.1-beta-src.tar.gz
--------------------------------------------------------------------------------
/archive/dbforbix-2.1-beta-win.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.1-beta-win.zip
--------------------------------------------------------------------------------
/archive/dbforbix-2.2-beta-linux64.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.2-beta-linux64.tar.gz
--------------------------------------------------------------------------------
/archive/dbforbix-2.2-beta-src.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.2-beta-src.tar.gz
--------------------------------------------------------------------------------
/archive/dbforbix-2.2-beta-win.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.2-beta-win.zip
--------------------------------------------------------------------------------
/archive/dbforbix-2.4-linux64.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.4-linux64.tar.gz
--------------------------------------------------------------------------------
/archive/dbforbix-2.4-src.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.4-src.tar.gz
--------------------------------------------------------------------------------
/archive/dbforbix-2.4-win.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/dbforbix-2.4-win.zip
--------------------------------------------------------------------------------
/archive/items/db2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | SELECT db_size FROM systools.stmg_dbsize_info;
5 | SELECT db_capacity FROM systools.stmg_dbsize_info;
6 | select service_level concat ' FP' concat fixpack_num from sysibmadm.env_inst_info;
7 | select inst_name from sysibmadm.env_inst_info;
8 | Select PRODUCT_NAME from sysibmadm.snapdbm;
9 | Select DB_NAME from sysibmadm.snapdb;
10 |
11 | Select SERVICE_LEVEL from sysibmadm.snapdbm;
12 |
13 | Select REM_CONS_IN + LOCAL_CONS from sysibmadm.snapdbm;
14 |
15 | Select sum(POOL_CUR_SIZE) from sysibmadm.SNAPDBM_MEMORY_POOL;
16 |
17 | Select TOTAL_CONS from sysibmadm.snapdb;
18 |
19 | Select TOTAL_LOG_USED *1. / TOTAL_LOG_AVAILABLE * 100. from sysibmadm.snapdb;
20 |
21 | Select NUM_INDOUBT_TRANS from sysibmadm.snapdb;
22 |
23 | Select X_LOCK_ESCALS from sysibmadm.snapdb;
24 |
25 | Select LOCK_ESCALS from sysibmadm.snapdb;
26 |
27 | Select LOCK_TIMEOUTS from sysibmadm.snapdb;
28 |
29 | Select DEADLOCKS from sysibmadm.snapdb;
30 |
31 | Select LAST_BACKUP from sysibmadm.snapdb;
32 |
33 | select DB_STATUS from sysibmadm.snapdb;
34 |
35 | select DB2_STATUS from sysibmadm.snapdbm;
36 |
37 | select case POOL_INDEX_L_READS when 0 then 1 else (POOL_INDEX_L_READS * 1. - POOL_INDEX_P_READS * 1.) / POOL_INDEX_L_READS end * 100. from sysibmadm.snapdb;
38 |
39 | select case POOL_DATA_L_READS when 0 then 1 else (POOL_DATA_L_READS * 1. - POOL_DATA_P_READS * 1.) / POOL_DATA_L_READS end *100. from sysibmadm.snapdb;
40 |
41 | select case TOTAL_SORTS when 0 then 0 else SORT_OVERFLOWS *1. / TOTAL_SORTS *1. end * 100. from sysibmadm.snapdb;
42 |
43 | select COALESCE(AGENTS_WAITING_TOP,0) from sysibmadm.snapdbm;
44 |
45 | Select ROWS_UPDATED from sysibmadm.snapdb;
46 | Select ROWS_INSERTED from sysibmadm.snapdb;
47 | Select ROWS_SELECTED from sysibmadm.snapdb;
48 | Select ROWS_DELETED from sysibmadm.snapdb;
49 | Select SELECT_SQL_STMTS from sysibmadm.snapdb;
50 | Select STATIC_SQL_STMTS from sysibmadm.snapdb;
51 | Select DYNAMIC_SQL_STMTS from sysibmadm.snapdb;
52 | Select ROLLBACK_SQL_STMTS from sysibmadm.snapdb;
53 | Select COMMIT_SQL_STMTS from sysibmadm.snapdb;
54 |
55 | select case POOL_TEMP_INDEX_L_READS when 0 then 1 else (POOL_TEMP_INDEX_L_READS * 1. - POOL_TEMP_INDEX_P_READS * 1.) / POOL_TEMP_INDEX_L_READS end * 100 from sysibmadm.snapdb;
56 |
57 | select case POOL_TEMP_DATA_L_READS when 0 then 1 else (POOL_TEMP_DATA_L_READS * 1. - POOL_TEMP_DATA_P_READS * 1.) / POOL_TEMP_DATA_L_READS end * 100. from sysibmadm.snapdb;
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/archive/items/mysql.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | SHOW VARIABLES LIKE "version"
5 |
6 | SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema','performance_schema')
7 | SELECT table_schema, COUNT(*) AS tablecount FROM information_schema.TABLES WHERE table_schema NOT IN ('information_schema','performance_schema') GROUP BY table_schema
8 | SELECT table_schema "database", SUM(data_length) "size" FROM INFORMATION_SCHEMA.TABLES WHERE table_schema NOT IN ('information_schema','performance_schema') GROUP BY table_schema
9 | SELECT table_schema "database", SUM(index_length) "size" FROM INFORMATION_SCHEMA.TABLES WHERE table_schema NOT IN ('information_schema','performance_schema') GROUP BY table_schema
10 | SELECT table_schema "database", SUM(data_free) "free" FROM INFORMATION_SCHEMA.TABLES WHERE table_schema NOT IN ('information_schema','performance_schema') GROUP BY table_schema
11 |
12 | SHOW VARIABLES
13 |
14 | SHOW GLOBAL STATUS
15 | SHOW GLOBAL STATUS
16 | SHOW GLOBAL STATUS
17 | SHOW GLOBAL STATUS
18 |
19 | SHOW GLOBAL STATUS
20 | SHOW GLOBAL STATUS
21 | SHOW GLOBAL STATUS
22 |
23 |
24 |
--------------------------------------------------------------------------------
/archive/items/oracle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | SELECT DISTINCT instance_name FROM v$instance
5 | SELECT DISTINCT instance_name,status FROM v$instance
6 |
7 | SELECT tablespace_name FROM dba_tablespaces WHERE contents NOT IN ('TEMPORARY')
8 | SELECT tablespace_name,SUM(NVL(bytes,0)) AS bytes FROM dba_data_files GROUP BY tablespace_name
9 | SELECT tablespace_name,SUM(NVL(bytes,0)) AS bytes, SUM(maxbytes) AS maxbytes FROM dba_data_files GROUP BY tablespace_name
10 |
11 | SELECT ROUND((sysdate-startup_time)*86400) retvalue FROM v$instance
12 | SELECT ROUND((sysdate-startup_time)*86400) retvalue FROM v$instance
13 |
14 | SELECT username||' '|| lock_date ||' '|| account_status
15 | FROM dba_users
16 | where ACCOUNT_STATUS like 'EXPIRED(GRACE)' or ACCOUNT_STATUS like 'LOCKED(TIMED)'
17 |
18 | SELECT COUNT(*) procnum from v$process
19 | select username "username",
20 | to_char(timestamp,'DD-MON-YYYY HH24:MI:SS') "time_stamp",
21 | action_name "statement",
22 | os_username "os_username",
23 | userhost "userhost",
24 | returncode||decode(returncode,'1004','-Wrong Connection','1005','-NULL Password','1017','-Wrong Password','1045','-Insufficient Priviledge','0','-Login Accepted','--') "returncode"
25 | from sys.dba_audit_session
26 | where (sysdate - timestamp)*24 < 1 and returncode <> 0
27 | order by timestamp
28 |
29 | SELECT SUM(misses) FROM V$LATCH
30 | SELECT namespace, gethitratio*100 get_pct FROM v$librarycache
31 | SELECT namespace, pins/(pins+reloads)*100 "pin_hit_ratio" FROM v$librarycache WHERE (pins+reloads)!=0
32 |
33 | SELECT COUNT(*) AS c FROM dba_users WHERE account_status IN('EXPIRED(GRACE)','LOCKED(TIMED)')
34 | SELECT COUNT(*) FROM v$session
35 | SELECT COUNT(*) FROM v$session WHERE type='BACKGROUND'
36 | SELECT COUNT(*) FROM v$session WHERE type!='BACKGROUND' AND status='ACTIVE'
37 | SELECT REPLACE(name,' ','_'),value FROM sys.v_$sysstat WHERE name IN ('user I/O wait time','physical read total bytes','physical write total bytes','lob reads','lob writes')
38 | SELECT sn.USERNAME ||'@'||sn.machine,
39 | '|SID->' || m.SID,
40 | '|Serial->'|| sn.SERIAL#,
41 | '|Lock Type->'||m.TYPE,
42 | decode(LMODE,
43 | 1, 'Null',
44 | 2, 'Row-S (SS)',
45 | 3, 'Row-X (SX)',
46 | 4, 'Share',
47 | 5, 'S/Row-X (SSX)',
48 | 6, 'Exclusive') lock_type,
49 | decode(REQUEST,
50 | 0, 'None',
51 | 1, 'Null',
52 | 2, 'Row-S (SS)',
53 | 3, 'Row-X (SX)',
54 | 4, 'Share',
55 | 5, 'S/Row-X (SSX)',
56 | 6, 'Exclusive') lock_requested,
57 | '|Time (Sec)->'||m.CTIME "Time(sec)",
58 | '|ID1->'||m.ID1,
59 | '|ID2->'||m.ID2,
60 | '|SQL Text->'||t.SQL_TEXT
61 | from v$session sn,
62 | v$lock m ,
63 | v$sqltext t
64 | where t.ADDRESS =sn.SQL_ADDRESS
65 | and t.HASH_VALUE =sn.SQL_HASH_VALUE
66 | and ((sn.SID =m.SID and m.REQUEST !=0)
67 | or (sn.SID =m.SID and m.REQUEST =0 and LMODE !=4 and (ID1, ID2) in
68 | (select s.ID1, s.ID2
69 | from v$lock S
70 | where REQUEST !=0
71 | and s.ctime > 5
72 | and s.ID1 =m.ID1
73 | and s.ID2 =m.ID2)))
74 | order by sn.USERNAME, sn.SID, t.PIECE
75 |
76 |
77 | SELECT name,value FROM v$parameter WHERE name IN ('processes','sessions')
78 | SELECT SUM(NVL(bytes,0)) val FROM dba_data_files
79 |
80 | SELECT SUM(NVL(a.bytes, 0) - NVL(f.bytes, 0)) val FROM sys.dba_tablespaces d,
81 | (SELECT tablespace_name, SUM(bytes) bytes FROM dba_data_files GROUP BY tablespace_name) a,
82 | (SELECT tablespace_name, SUM(bytes) bytes FROM dba_free_space GROUP BY tablespace_name) f
83 | WHERE d.tablespace_name = a.tablespace_name(+)
84 | AND d.tablespace_name = f.tablespace_name(+)
85 | AND NOT (
86 | d.extent_management LIKE 'LOCAL'
87 | AND d.contents LIKE 'TEMPORARY'
88 | )
89 |
90 | SELECT name,value FROM v$parameter WHERE name IN ('sga_max_size','memory_target','memory_max_target','shared_pool_reserved_size','db_block_size','log_buffer','db_files','parallel_server_instances','cluster_database_instances','db_flashback_retention_target','distributed_lock_timeout','create_bitmap_area_size','bitmap_merge_area_size','hash_area_size','sort_area_size')
91 | SELECT ' - DB_NAME = <b>'||SYS_CONTEXT ('USERENV', 'DB_NAME') ||'</b> - INSTANCE_NAME = <b>' ||SYS_CONTEXT ('USERENV', 'INSTANCE_NAME')||'</b>' FROM dual
92 |
93 | SELECT ' - DB_NAME = <b>'||SYS_CONTEXT ('USERENV', 'DB_NAME') ||'</b> - INSTANCE_NAME = <b>' ||SYS_CONTEXT ('USERENV', 'INSTANCE_NAME')||'</b>' FROM dual
94 |
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------
/archive/items/param.dtd:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 |
25 |
31 |
38 |
--------------------------------------------------------------------------------
/archive/items/pgsql.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | SELECT SUM(numbackends) FROM pg_stat_database
5 | SELECT SUM(tup_returned) FROM pg_stat_database
6 | SELECT SUM(tup_fetched) FROM pg_stat_database
7 | SELECT SUM(tup_inserted) FROM pg_stat_database
8 | SELECT SUM(tup_updated) FROM pg_stat_database
9 | SELECT SUM(tup_deleted) FROM pg_stat_database
10 | SELECT SUM(xact_commit) FROM pg_stat_database
11 | SELECT SUM(xact_rollback) FROM pg_stat_database
12 |
13 | SELECT COUNT(*) FROM pg_locks WHERE mode='ExclusiveLock'
14 | SELECT COUNT(*) FROM pg_locks WHERE mode='AccessExclusiveLock'
15 | SELECT COUNT(*) FROM pg_locks WHERE mode='AccessShareLock'
16 | SELECT COUNT(*) FROM pg_locks WHERE mode='RowShareLock'
17 | SELECT COUNT(*) FROM pg_locks WHERE mode='RowExclusiveLock'
18 | SELECT COUNT(*) FROM pg_locks WHERE mode='ShareUpdateExclusiveLock'
19 | SELECT COUNT(*) FROM pg_locks WHERE mode='ShareRowExclusiveLock'
20 |
21 | SELECT checkpoints_timed FROM pg_stat_bgwriter
22 | SELECT checkpoints_req FROM pg_stat_bgwriter
23 | SELECT buffers_checkpoint FROM pg_stat_bgwriter
24 | SELECT buffers_clean FROM pg_stat_bgwriter
25 | SELECT maxwritten_clean FROM pg_stat_bgwriter
26 | SELECT buffers_backend FROM pg_stat_bgwriter
27 | SELECT buffers_alloc FROM pg_stat_bgwriter
28 |
29 |
30 |
--------------------------------------------------------------------------------
/archive/lib/db2java.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/archive/lib/db2java.zip
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
--------------------------------------------------------------------------------
/dist/conf/config.properties:
--------------------------------------------------------------------------------
1 | #frequency of update config in seconds
2 | DBforBIX.UpdateConfigTimeout=120
3 |
4 | #MaxThreadNumber should be >= than the number of your databases
5 |
6 |
7 | #
8 | #### Configuration of Connection pool ###
9 | #
10 | ## Maximum number of active connection inside pool
11 | #
12 | ## Login Timeout
13 | Pool.LoginTimeOut=15
14 |
15 | #
16 | ### Zabbix servers ###
17 | # A list of Zabbix servers, change the name after first dot to add more
18 | # Zabbix Server
19 | ZabbixServer.Zabbix1.Address=
20 | # Zabbix server port (trapper port)
21 | ZabbixServer.Zabbix1.Port=10051
22 | # Name of Zabbix proxy (you should create this name on your Zabbix Web Interface)
23 | # It will be used to get hosts and items configurations
24 | ZabbixServer.Zabbix1.ProxyName=DBforBIX
25 | # Item key suffix to find its configuration on Zabbix Server
26 | ZabbixServer.Zabbix1.ConfigSuffix=DBforBIX.config
27 | # List of comma separated databases
28 | # This names also should be defined in your Template/Host macros {$DSN} in Zabbix Web Interface
29 | # See zabbix templates attached for further details
30 | ZabbixServer.Zabbix1.DBList=MYSQLDB1,ORADB1
31 | ###
32 | #
33 |
34 |
35 |
36 |
37 |
38 | ### Database servers ###
39 |
40 | ###MySQL Server
41 | #define here your connection string for each database
42 | #on the following example we are treating MySQL
43 | DB.MYSQLDB1.Url=jdbc:mysql://:/
44 | DB.MYSQLDB1.Instance=
45 | DB.MYSQLDB1.User=
46 | DB.MYSQLDB1.Password=
47 | DB.MYSQLDB1.Type=mysql
48 | DB.MYSQLDB1.MaxActive=10
49 | DB.MYSQLDB1.MaxWaitMillis=10000
50 | DB.MYSQLDB1.QueryTimeout=15
51 | DB.MYSQLDB1.MaxIdle=1
52 |
53 | ###Oracle Server
54 | #define here your connection string for each database
55 | #on the following example we are treating Oracle
56 | DB.ORADB1.Type=oracle
57 | DB.ORADB1.Instance=
58 | DB.ORADB1.Url=jdbc:oracle:thin:@::
59 | DB.ORADB1.User=
60 | DB.ORADB1.Password=
61 | DB.ORADB1.MaxActive=10
62 | DB.ORADB1.MaxWaitMillis=10000
63 | DB.ORADB1.QueryTimeout=15
64 | DB.ORADB1.MaxIdle=1
65 |
66 |
67 |
68 | # Define your database connections
69 | # Not tested yet
70 | #DB.DB1.Type=pgsql
71 | #DB.DB1.Name=PostgreSVR
72 | #DB.DB1.Url=jdbc:postgresql://localhost:5432/
73 | #DB.DB1.User=user
74 | #DB.DB1.Password=passwd
75 |
76 | #DB.DB3.Type=mssql
77 | #DB.DB3.Name=MSSQLSRV
78 | #DB.DB3.Url=jdbc:jtds:sqlserver://localhost:3678/INSTANCENAME
79 | #DB.DB3.User=user
80 | #DB.DB3.Password=passwd
81 |
82 |
--------------------------------------------------------------------------------
/dist/dbforbix-3.0-linux64.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/dbforbix-3.0-linux64.tar.gz
--------------------------------------------------------------------------------
/dist/dbforbix-3.0-src.tar.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/dbforbix-3.0-src.tar.gz
--------------------------------------------------------------------------------
/dist/dbforbix-3.0-win.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/dbforbix-3.0-win.zip
--------------------------------------------------------------------------------
/dist/dbforbix.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #
3 | java -jar dbforbix_run.jar -b `pwd` -a start
4 |
--------------------------------------------------------------------------------
/dist/dbforbix_run.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/dbforbix_run.jar
--------------------------------------------------------------------------------
/dist/dbforbix_run_3.0-beta.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/dbforbix_run_3.0-beta.jar
--------------------------------------------------------------------------------
/dist/items/param.dtd:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 |
25 |
31 |
38 |
--------------------------------------------------------------------------------
/dist/jar/dbforbix.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/dbforbix.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-cli-1.3.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-cli-1.3.1.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-codec-1.10.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-codec-1.10.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-collections4-4.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-collections4-4.1.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-configuration2-2.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-configuration2-2.1.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-daemon-1.0.15.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-daemon-1.0.15.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-dbcp2-2.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-dbcp2-2.1.1.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-lang3-3.5.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-lang3-3.5.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-logging-1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-logging-1.2.jar
--------------------------------------------------------------------------------
/dist/jar/lib/commons-pool2-2.4.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/commons-pool2-2.4.2.jar
--------------------------------------------------------------------------------
/dist/jar/lib/dom4j-1.6.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/dom4j-1.6.1.jar
--------------------------------------------------------------------------------
/dist/jar/lib/fastjson-1.2.8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/fastjson-1.2.8.jar
--------------------------------------------------------------------------------
/dist/jar/lib/h2-1.4.193.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/h2-1.4.193.jar
--------------------------------------------------------------------------------
/dist/jar/lib/log4j-1.2.17.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/jar/lib/log4j-1.2.17.jar
--------------------------------------------------------------------------------
/dist/linux/dbforbix.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/dbforbix.jar
--------------------------------------------------------------------------------
/dist/linux/init.d/dbforbix:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # This is the init script for starting up the
3 | # Orabbix daemon
4 | #
5 | # chkconfig: 345 91 10
6 | # description: Starts and stops the orabbix daemon.
7 | # processname: orabbix
8 | #
9 | # Source function library.
10 | . /etc/rc.d/init.d/functions
11 |
12 | # Get config.
13 | . /etc/sysconfig/network
14 |
15 | # Check that networking is up.
16 | [ "${NETWORKING}" = "no" ] && exit 0
17 |
18 | dbforbix=/opt/dbforbix
19 | startup=${dbforbix}/dbforbix.sh start
20 |
21 | start(){
22 | echo -n "Starting DBforBIX service:"
23 | cd ${dbforbix}
24 | $startup
25 | RETVAL=$?
26 | echo
27 | [ $RETVAL -eq 0 ] && touch /var/lock/subsys/dbforbix
28 | return $RETVAL
29 | }
30 |
31 | stop(){
32 | echo -n "Stopping DBforBIX service:"
33 | pid=`ps -ef |grep java |grep -1 dbforbix | awk '{ print $2 }'`
34 | kill `cat ${dbforbix}/logs/dbforbix.pid`
35 | RETVAL=$?
36 | echo
37 | [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/dbforbix
38 | return $RETVAL
39 | }
40 |
41 | restart(){
42 | stop
43 | start
44 | }
45 |
46 | status(){
47 | numproc=`ps -ef | grep java | grep -i dbforbix | grep -v grep | wc -l`
48 | if [ "$numproc" -gt 0 ]; then
49 | echo "DBforBIX is running"
50 | else
51 | echo "DBforBIX is stopped"
52 | fi
53 | }
54 |
55 | # See how we were called.
56 | case "$1" in
57 | start)
58 | start
59 | ;;
60 | stop)
61 | stop
62 | ;;
63 | status)
64 | status
65 | ;;
66 | restart)
67 | restart
68 | ;;
69 | *)
70 | echo "Usage: $0 {start|stop|status|restart}"
71 | exit 1
72 | esac
73 | exit $RETVAL
74 |
75 |
--------------------------------------------------------------------------------
/dist/linux/items/param.dtd:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 |
25 |
31 |
38 |
--------------------------------------------------------------------------------
/dist/linux/lib/commons-cli-1.3.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-cli-1.3.1.jar
--------------------------------------------------------------------------------
/dist/linux/lib/commons-codec-1.10.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-codec-1.10.jar
--------------------------------------------------------------------------------
/dist/linux/lib/commons-collections4-4.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-collections4-4.1.jar
--------------------------------------------------------------------------------
/dist/linux/lib/commons-configuration2-2.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-configuration2-2.1.jar
--------------------------------------------------------------------------------
/dist/linux/lib/commons-daemon-1.0.15.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-daemon-1.0.15.jar
--------------------------------------------------------------------------------
/dist/linux/lib/commons-dbcp2-2.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-dbcp2-2.1.1.jar
--------------------------------------------------------------------------------
/dist/linux/lib/commons-lang3-3.5.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-lang3-3.5.jar
--------------------------------------------------------------------------------
/dist/linux/lib/commons-logging-1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-logging-1.2.jar
--------------------------------------------------------------------------------
/dist/linux/lib/commons-pool2-2.4.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/commons-pool2-2.4.2.jar
--------------------------------------------------------------------------------
/dist/linux/lib/dom4j-1.6.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/dom4j-1.6.1.jar
--------------------------------------------------------------------------------
/dist/linux/lib/fastjson-1.2.8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/fastjson-1.2.8.jar
--------------------------------------------------------------------------------
/dist/linux/lib/h2-1.4.193.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/h2-1.4.193.jar
--------------------------------------------------------------------------------
/dist/linux/lib/log4j-1.2.17.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/linux/lib/log4j-1.2.17.jar
--------------------------------------------------------------------------------
/dist/log4j.properties:
--------------------------------------------------------------------------------
1 | ## Set root logger level to DEBUG and its only appender to DBforBIX.
2 | log4j.rootLogger=DEBUG,DBforBIX,console
3 | #
4 | ## DBforBIX is set to be a ConsoleAppender.
5 | log4j.appender.DBforBIX=org.apache.log4j.RollingFileAppender
6 | log4j.appender.DBforBIX.File=/var/log/DBforBIX/DBforBIX.log4j
7 | #
8 | ## DBforBIX uses PatternLayout.
9 | log4j.appender.DBforBIX.layout=org.apache.log4j.PatternLayout
10 | log4j.appender.DBforBIX.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] [%t[%M(%F:%L)]]: %m%n
11 |
12 | log4j.appender.console=org.apache.log4j.ConsoleAppender
13 | log4j.appender.console.layout=org.apache.log4j.PatternLayout
14 | log4j.appender.console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] [%t[%M(%F:%L)]]: %m%n
15 |
--------------------------------------------------------------------------------
/dist/win/dbforbix.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/dbforbix.jar
--------------------------------------------------------------------------------
/dist/win/dbforbix32.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/dbforbix32.exe
--------------------------------------------------------------------------------
/dist/win/dbforbix64.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/dbforbix64.exe
--------------------------------------------------------------------------------
/dist/win/dbforbixw.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/dbforbixw.exe
--------------------------------------------------------------------------------
/dist/win/items/param.dtd:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 |
25 |
31 |
38 |
--------------------------------------------------------------------------------
/dist/win/lib/commons-cli-1.3.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-cli-1.3.1.jar
--------------------------------------------------------------------------------
/dist/win/lib/commons-codec-1.10.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-codec-1.10.jar
--------------------------------------------------------------------------------
/dist/win/lib/commons-collections4-4.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-collections4-4.1.jar
--------------------------------------------------------------------------------
/dist/win/lib/commons-configuration2-2.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-configuration2-2.1.jar
--------------------------------------------------------------------------------
/dist/win/lib/commons-daemon-1.0.15.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-daemon-1.0.15.jar
--------------------------------------------------------------------------------
/dist/win/lib/commons-dbcp2-2.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-dbcp2-2.1.1.jar
--------------------------------------------------------------------------------
/dist/win/lib/commons-lang3-3.5.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-lang3-3.5.jar
--------------------------------------------------------------------------------
/dist/win/lib/commons-logging-1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-logging-1.2.jar
--------------------------------------------------------------------------------
/dist/win/lib/commons-pool2-2.4.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/commons-pool2-2.4.2.jar
--------------------------------------------------------------------------------
/dist/win/lib/dom4j-1.6.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/dom4j-1.6.1.jar
--------------------------------------------------------------------------------
/dist/win/lib/fastjson-1.2.8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/fastjson-1.2.8.jar
--------------------------------------------------------------------------------
/dist/win/lib/h2-1.4.193.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/h2-1.4.193.jar
--------------------------------------------------------------------------------
/dist/win/lib/log4j-1.2.17.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/dist/win/lib/log4j-1.2.17.jar
--------------------------------------------------------------------------------
/ext_jars/commons-cli-1.3.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-cli-1.3.1.jar
--------------------------------------------------------------------------------
/ext_jars/commons-codec-1.10.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-codec-1.10.jar
--------------------------------------------------------------------------------
/ext_jars/commons-collections4-4.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-collections4-4.1.jar
--------------------------------------------------------------------------------
/ext_jars/commons-configuration2-2.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-configuration2-2.1.jar
--------------------------------------------------------------------------------
/ext_jars/commons-daemon-1.0.15.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-daemon-1.0.15.jar
--------------------------------------------------------------------------------
/ext_jars/commons-dbcp2-2.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-dbcp2-2.1.1.jar
--------------------------------------------------------------------------------
/ext_jars/commons-lang3-3.5.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-lang3-3.5.jar
--------------------------------------------------------------------------------
/ext_jars/commons-logging-1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-logging-1.2.jar
--------------------------------------------------------------------------------
/ext_jars/commons-pool2-2.4.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/commons-pool2-2.4.2.jar
--------------------------------------------------------------------------------
/ext_jars/dom4j-1.6.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/dom4j-1.6.1.jar
--------------------------------------------------------------------------------
/ext_jars/fastjson-1.2.20.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/fastjson-1.2.20.jar
--------------------------------------------------------------------------------
/ext_jars/log4j-1.2.17.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/log4j-1.2.17.jar
--------------------------------------------------------------------------------
/ext_jars/mysql-connector-java-5.1.38.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/mysql-connector-java-5.1.38.jar
--------------------------------------------------------------------------------
/ext_jars/ojdbc6.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/ojdbc6.jar
--------------------------------------------------------------------------------
/ext_jars/ojdbc7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/ojdbc7.jar
--------------------------------------------------------------------------------
/ext_jars/ojdbc7_g.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/ojdbc7_g.jar
--------------------------------------------------------------------------------
/ext_jars/ojdbc8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/ojdbc8.jar
--------------------------------------------------------------------------------
/ext_jars/postgresql-9.4.1207.jre7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/postgresql-9.4.1207.jre7.jar
--------------------------------------------------------------------------------
/ext_jars/sqljdbc42.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/ext_jars/sqljdbc42.jar
--------------------------------------------------------------------------------
/install/linux/dbforbix.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | ### BEGIN INIT INFO
4 | # Provides: dbforbix
5 | # Required-Start: $syslog $network $time
6 | # Required-Stop: $syslog $network
7 | # Should-Start: postgresql mysql clamav-daemon greylist spamassassin
8 | # Should-Stop: postgresql mysql clamav-daemon greylist spamassassin
9 | # Default-Start: 2 3 4 5
10 | # Default-Stop: 0 1 6
11 | # Short-Description: dbforbix daemon
12 | # Description: dbforbix daemon
13 | ### END INIT INFO
14 |
15 | # Setup variables
16 | DESC="DBforBIX monitoring daemon"
17 | EXEC=`whereis -b -B /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin -f jsvc | awk '{ print $2;}'`
18 | BASEDIR="/opt/dbforbix"
19 | #CLASS_PATH="$BASEDIR/dbforbix.jar":"$BASEDIR/.":"$BASEDIR/lib/commons-cli-1.3.1.jar":"$BASEDIR/lib/httpcore-4.4.4.jar":"$BASEDIR/lib/commons-codec-1.9.jar":"$BASEDIR/lib/jtds-1.3.1.jar":"$BASEDIR/lib/commons-collections-3.2.2.jar":"$BASEDIR/lib/log4j-1.2.17.jar":"$BASEDIR/lib/commons-configuration-1.10.jar":"$BASEDIR/lib/logback-classic-1.0.13.jar":"$BASEDIR/lib/commons-daemon-1.0.15.jar":"$BASEDIR/lib/logback-core-1.0.13.jar":"$BASEDIR/lib/commons-dbcp-1.4.jar":"$BASEDIR/lib/mysql-connector-java-5.1.38.jar":"$BASEDIR/lib/commons-lang-2.6.jar":"$BASEDIR/lib/ojdbc6.jar":"$BASEDIR/lib/commons-logging-1.2.jar":"$BASEDIR/lib/postgresql-9.4.1207.jre7.jar":"$BASEDIR/lib/commons-pool-1.6.jar":"$BASEDIR/lib/slf4j-api-1.7.5.jar":"$BASEDIR/lib/dom4j-1.6.1.jar":"$BASEDIR/lib/xml-apis-1.0.b2.jar":"$BASEDIR/lib/fastjson-1.2.8.jar":"$BASEDIR/lib/zabbix-sender-0.0.1.jar":"$BASEDIR/lib/httpclient-4.5.2.jar"
20 | CLASS_PATH="$BASEDIR/*":"$BASEDIR/lib/*"
21 |
22 | CLASS=com.smartmarmot.dbforbix.DBforBix
23 | USER=dbforbix
24 | PID=/tmp/dbforbix.pid
25 | LOG_OUT=/var/log/dbforbix.out
26 | LOG_ERR=/var/log/dbforbix.err
27 |
28 | if [ "$EXEC" = "" ]; then
29 | echo "jsvc not found, terminating"
30 | exit 3
31 | fi
32 |
33 | if [ -f /opt/dbforbix/java_env ]; then
34 | . /opt/dbforbix/java_env
35 | fi
36 |
37 | # try to find java home if not defined
38 | if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then
39 | # echo "JAVA_HOME and JRE_HOME not defined"
40 | if [ -x /etc/alternatives/java ]; then
41 | LINK=`readlink -f /etc/alternatives/java`
42 | LINK="${LINK%bin/java}"
43 | export JAVA_HOME=$LINK
44 | fi
45 | fi
46 |
47 | do_exec()
48 | {
49 | CURDIR=`pwd`
50 | JSVCV=`$EXEC -? | grep "jsvc (Apache Commons Daemon)" | awk '{ print $5;}'`
51 | JSVCV=${JSVCV%-*}
52 | MAIN=${JSVCV%%.*}
53 | MINOR=${JSVCV%.*}
54 | MINOR=${MINOR#*.}
55 | PATCH=${JSVCV##*.}
56 |
57 | # they added -cwd with patchlevel 1.0.11
58 | if [ $PATCH -ge 11 ]; then
59 | $EXEC -cwd $BASEDIR -Xmx64m -debug -procname dbforbix -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS $BASEDIR
60 | else
61 | echo "Using compartibility for jsvc < 1.0.11"
62 | cd $BASEDIR
63 | echo $BASEDIR
64 | $EXEC -Xmx64m -debug -procname dbforbix -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS $BASEDIR
65 | cd $CURDIR
66 | fi
67 | }
68 |
69 | case "$1" in
70 | start)
71 | echo -n "Starting $DESC... "
72 | do_exec ""
73 | echo "DONE"
74 | ;;
75 |
76 | stop)
77 | echo -n "Stopping $DESC... "
78 | do_exec "-stop"
79 | echo "DONE"
80 | ;;
81 |
82 | restart)
83 | if [ -f "$PID" ]; then
84 | do_exec "-stop"
85 | sleep 1
86 | do_exec
87 | else
88 | echo "service not running, will do nothing"
89 | exit 1
90 | fi
91 | ;;
92 |
93 | status)
94 | echo -n "Status of $DESC: "
95 | PCOUNT=`ps aux | grep "dbforbix" | wc -l`
96 | if [ "$PCOUNT" -gt 3 ]; then
97 | echo "most likely runnnig"
98 | exit 0
99 | else
100 | echo "most likely stopped"
101 | exit 3
102 | fi
103 | ;;
104 |
105 | *)
106 | echo "usage: $0 {start|stop|restart}" >&2
107 | exit 9
108 | ;;
109 | esac
110 |
--------------------------------------------------------------------------------
/install/linux/systemd/dbforbix-nojsvc.service:
--------------------------------------------------------------------------------
1 | [Unit]
2 | Description=DBforBIX Monitoring Agent
3 | After=syslog.target network.target
4 |
5 | [Service]
6 | Type=simple
7 | User=
8 | Group=
9 | Environment=JAVA_HOME=
10 | WorkingDirectory=/opt/dbforbix
11 | ExecStart=/bin/java -jar dbforbix.jar -a start -C /opt/dbforbix
12 | SuccessExitStatus=143
13 |
14 | [Install]
15 | WantedBy=multi-user.target
16 |
--------------------------------------------------------------------------------
/install/linux/systemd/dbforbix.service:
--------------------------------------------------------------------------------
1 | [Unit]
2 | Description=DBforBIX Monitoring Agent
3 | After=syslog.target network.target
4 | Conflicts=
5 |
6 | [Service]
7 | Type=forking
8 | PIDFile=/tmp/dbforbix.pid
9 | ExecStart=/opt/dbforbix/dbforbix.sh start
10 | ExecReload=/opt/dbforbix/dbforbix.sh restart
11 | ExecStop=/opt/dbforbix/dbforbix.sh stop
12 |
13 | [Install]
14 | WantedBy=multi-user.target
--------------------------------------------------------------------------------
/install/win/install.cmd:
--------------------------------------------------------------------------------
1 | @echo off
2 |
3 | SET mypath=%~dp0
4 |
5 | if "%PROCESSOR_ARCHITECTURE%"=="AMD64" goto 64BIT
6 | echo Installing 32bit service
7 | %mypath%dbforbix32.exe //IS//DBforBIX --DisplayName="DBforBIX Zabbix Agent" --StartPath=%mypath% --LogPath="logs" --Classpath "dbforbix.jar;.;lib/commons-cli-1.3.1.jar;lib/httpcore-4.4.4.jar;lib/commons-codec-1.9.jar;lib/jtds-1.3.1.jar;lib/commons-collections-3.2.2.jar;lib/log4j-1.2.17.jar;lib/commons-configuration-1.10.jar;lib/logback-classic-1.0.13.jar;lib/commons-daemon-1.0.15.jar;lib/logback-core-1.0.13.jar;lib/commons-dbcp-1.4.jar;lib/mysql-connector-java-5.1.38.jarcommons-lang-2.6.jar;lib/ojdbc6.jar;lib/commons-logging-1.2.jar;lib/postgresql-9.4.1207.jre7.jar;lib/commons-pool-1.6.jar;lib/slf4j-api-1.7.5.jar;lib/dom4j-1.6.1.jar;lib/xml-apis-1.0.b2.jar;lib/fastjson-1.2.8.jar;lib/zabbix-sender-0.0.1.jar;lib/httpclient-4.5.2.jar" --Install=%mypath%dbforbix32.exe --Jvm=auto --StartMode=jvm --StartClass=com.smartmarmot.dbforbix.DBforBix --StartMethod=start --StopMode=jvm --StopClass=com.smartmarmot.dbforbix.DBforBIX --StopMethod=stop
8 | goto END
9 |
10 | :64BIT
11 | echo Installing 64bit service
12 | %mypath%dbforbix64.exe //IS//DBforBIX --DisplayName="DBforBIX Zabbix Agent" --StartPath=%mypath% --LogPath="logs" --Classpath "dbforbix.jar;.;lib/commons-cli-1.3.1.jar;lib/httpcore-4.4.4.jar;lib/commons-codec-1.9.jar;lib/jtds-1.3.1.jar;lib/commons-collections-3.2.2.jar;lib/log4j-1.2.17.jar;lib/commons-configuration-1.10.jar;lib/logback-classic-1.0.13.jar;lib/commons-daemon-1.0.15.jar;lib/logback-core-1.0.13.jar;lib/commons-dbcp-1.4.jar;lib/mysql-connector-java-5.1.38.jarcommons-lang-2.6.jar;lib/ojdbc6.jar;lib/commons-logging-1.2.jar;lib/postgresql-9.4.1207.jre7.jar;lib/commons-pool-1.6.jar;lib/slf4j-api-1.7.5.jar;lib/dom4j-1.6.1.jar;lib/xml-apis-1.0.b2.jar;lib/fastjson-1.2.8.jar;lib/zabbix-sender-0.0.1.jar;lib/httpclient-4.5.2.jar" --Install=%mypath%dbforbix64.exe --Jvm=auto --StartMode=jvm --StartClass=com.smartmarmot.dbforbix.DBforBix --StartMethod=start --StopMode=jvm --StopClass=com.smartmarmot.dbforbix.DBforBIX --StopMethod=stop
13 |
14 | :END
15 | pause
16 |
17 |
--------------------------------------------------------------------------------
/install/win/uninstall.cmd:
--------------------------------------------------------------------------------
1 | @echo off
2 |
3 | SET mypath=%~dp0
4 |
5 | if "%PROCESSOR_ARCHITECTURE%"=="AMD64" goto 64BIT
6 | echo Removing 32bit service
7 | %mypath%dbforbix32.exe //DS//DBforBIX
8 | goto END
9 |
10 | :64BIT
11 | echo Removing 64bit service
12 | %mypath%dbforbix64.exe //DS//DBforBIX
13 |
14 | :END
15 | pause
--------------------------------------------------------------------------------
/src/ObjectModel.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/smartmarmot/DBforBIX/8f2fda15733807bfe75cf8d7fe4863494d4a7447/src/ObjectModel.png
--------------------------------------------------------------------------------
/src/com/smartmarmot/common/Constants.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Andrea Dalle Vacche.
3 | *
4 | * This file is part of DBforBIX.
5 | *
6 | * DBforBIX is free software: you can redistribute it and/or modify it under the
7 | * terms of the GNU General Public License as published by the Free Software
8 | * Foundation, either version 3 of the License, or (at your option) any later
9 | * version.
10 | *
11 | * DBforBIX is distributed in the hope that it will be useful, but WITHOUT ANY
12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 | * details.
15 | *
16 | * You should have received a copy of the GNU General Public License along with
17 | * DBforBIX. If not, see .
18 | */
19 |
20 | package com.smartmarmot.common;
21 |
22 | public class Constants {
23 | private static final String VERSION = "Version 3.2.0-beta";
24 | public static final String BANNER = Constants.PROJECT_NAME + " " + VERSION;
25 | private static final String PROJECT_NAME = "DBforBix";
26 | }
--------------------------------------------------------------------------------
/src/com/smartmarmot/common/Persistence.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.common;
2 |
3 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
4 |
5 | public interface Persistence {
6 |
7 |
8 | public ZabbixItem pop();
9 |
10 | public Long size();
11 |
12 |
13 | public void push(ZabbixItem zi);
14 |
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/common/PersistentDB.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.common;
2 |
3 | import java.io.File;
4 | import java.sql.Connection;
5 | import java.sql.DriverManager;
6 | import java.sql.PreparedStatement;
7 | import java.sql.ResultSet;
8 | import java.sql.SQLException;
9 | import java.sql.Statement;
10 | import java.util.Collection;
11 |
12 | import org.apache.log4j.Level;
13 | import org.apache.log4j.Logger;
14 |
15 | import com.smartmarmot.dbforbix.config.Config;
16 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
17 |
18 |
19 | //H2 File Database Example shows about storing the database contents into file system.
20 |
21 |
22 | public class PersistentDB implements Persistence{
23 | private static PersistentDB instance;
24 |
25 | private static final String DB_DRIVER = "org.h2.Driver";
26 | //private static final String DB_CONNECTION = "jdbc:h2:/opt/dbforbix/persistentdb";
27 | private static String DB_CONNECTION = "jdbc:h2:";
28 | private static final String DB_USER = "";
29 | private static final String DB_PASSWORD = "";
30 | private static final Logger LOG = Logger.getLogger(StackSingletonPersistent.class);
31 | private String DIRECTORY = Config.getInstance().getSPDir();
32 |
33 | private void init() throws ClassNotFoundException {
34 | Class.forName("org.h2.Driver");
35 | //DeleteDbFiles.execute("~", "test", true);
36 |
37 | Config config = Config.getInstance();
38 | if (DIRECTORY.startsWith("./"))
39 | DIRECTORY = DIRECTORY.replaceFirst(".", config.getBasedir());
40 | DIRECTORY.replace("//","/");
41 |
42 |
43 | File file = new File(DIRECTORY);
44 | if (!file.exists()) {
45 | LOG.log(Level.ERROR, "PersistentDB - Persistence directory "+DIRECTORY +" does not exists");
46 | boolean success = (new File(DIRECTORY)).mkdirs();
47 | if (success) {
48 | LOG.log(Level.INFO, "PersistentDB - Persistence directory "+DIRECTORY +" created");
49 | } else{
50 | LOG.log(Level.ERROR, "PersistentDB - Persistence directory "+DIRECTORY +" creation failed");
51 | }
52 |
53 | }
54 | String url = PersistentDB.DB_CONNECTION+DIRECTORY+"itemdb";
55 | PersistentDB.DB_CONNECTION=url+";TRACE_LEVEL_FILE=1;TRACE_MAX_FILE_SIZE=5";
56 | Connection conn;
57 | try {
58 | LOG.log(Level.INFO,"PersistentDB - initialization");
59 | conn = DriverManager.getConnection(url + ";LOG=0");
60 | Statement stat = conn.createStatement();
61 | stat.execute("CREATE TABLE IF NOT EXISTS ITEM(IDX BIGINT PRIMARY KEY, "
62 | + "HOST VARCHAR(255), "
63 | + "KEY VARCHAR(255), "
64 | + "VALUE VARCHAR(4096),"
65 | + "CLOCK BIGINT)");
66 | conn.commit();
67 | stat.close();
68 | conn.close();
69 | LOG.log(Level.INFO,"PersistentDB - initialization completed");
70 | } catch (SQLException e) {
71 | // TODO Auto-generated catch block
72 | LOG.log(Level.ERROR,"PersistentDB - initialization error:"+e.getMessage());
73 | }
74 |
75 | }
76 |
77 |
78 |
79 | public static synchronized PersistentDB getInstance() {
80 | if (instance == null) {
81 | instance = new PersistentDB();
82 | try {
83 | instance.init();
84 | } catch (ClassNotFoundException e) {
85 | LOG.log(Level.ERROR, "PersistentDB - initialization error:"+e.getMessage());
86 | }
87 | }
88 | return instance;
89 | }
90 |
91 |
92 |
93 |
94 | public void push(ZabbixItem zItem) {
95 | Connection connection = getDBConnection();
96 | PreparedStatement insertPreparedStatement = null;
97 | PreparedStatement selectPreparedStatement = null;
98 |
99 | String InsertQuery = "INSERT INTO ITEM (IDX,HOST,KEY,VALUE,CLOCK) values" + "(?,?,?,?,?)";
100 | String SelectMaxIDX = "SELECT COALESCE(MAX(IDX),0) as IDX from ITEM";
101 | String SelectMaxQuery = "SELECT IDX,HOST,KEY,VALUE,CLOCK from ITEM where IDX=?";
102 | try {
103 | connection.setAutoCommit(false);
104 | selectPreparedStatement = connection.prepareStatement(SelectMaxIDX);
105 | ResultSet rs = selectPreparedStatement.executeQuery();
106 | Long maxIDX = 0L;
107 | while (rs.next()) {
108 | maxIDX = rs.getLong("IDX");
109 |
110 | }
111 | rs.close();
112 |
113 | if (maxIDX > 0L ){
114 | selectPreparedStatement = connection.prepareStatement(SelectMaxQuery);
115 | selectPreparedStatement.setLong(1, maxIDX);
116 | rs = selectPreparedStatement.executeQuery();
117 | while (rs.next()) {
118 | LOG.log(Level.DEBUG,"PersistentDB - Last item present in PersistentDB "+
119 | " IDX "+rs.getLong("IDX")+
120 | " Host="+rs.getString("HOST")+
121 | " Key="+rs.getString("KEY")+
122 | " Value="+rs.getString("VALUE")+
123 | " Clock="+rs.getLong("CLOCK"));
124 | }
125 | }
126 | rs.close();
127 | insertPreparedStatement = connection.prepareStatement(InsertQuery);
128 | insertPreparedStatement.setLong(1,maxIDX+1L);
129 | insertPreparedStatement.setString(2, zItem.getHost());
130 | insertPreparedStatement.setString(3, zItem.getKey());
131 | insertPreparedStatement.setString(4, zItem.getValue());
132 | insertPreparedStatement.setLong(5, zItem.getClock());
133 | insertPreparedStatement.executeUpdate();
134 | insertPreparedStatement.close();
135 |
136 |
137 | insertPreparedStatement.close();
138 | connection.commit();
139 | connection.close();
140 |
141 | } catch (SQLException e) {
142 | LOG.log(Level.ERROR, "PersistentDB - Error " + e.getLocalizedMessage());
143 | } catch (Exception e) {
144 | e.printStackTrace();
145 | } finally {
146 | try {
147 | connection.close();
148 | } catch (SQLException e) {
149 | // TODO Auto-generated catch block
150 | LOG.log(Level.ERROR, "PersistentDB - Error " + e.getMessage());
151 | }
152 | }
153 | }
154 |
155 |
156 | public Long size() {
157 | Connection connection = getDBConnection();
158 | PreparedStatement preparedStmt = null;
159 | String SelectMaxIDX = "SELECT COALESCE(MAX(IDX),0) as IDX from ITEM";
160 | Long maxIDX = 0L;
161 | try {
162 | connection.setAutoCommit(false);
163 | preparedStmt = connection.prepareStatement(SelectMaxIDX);
164 | ResultSet rs = preparedStmt.executeQuery();
165 | while (rs.next()) {
166 | maxIDX = rs.getLong("IDX");
167 |
168 | }
169 |
170 | rs.close();
171 | connection.close();
172 |
173 | } catch (SQLException e) {
174 | LOG.log(Level.ERROR, "PersistentDB - Error " + e.getLocalizedMessage());
175 | } catch (Exception e) {
176 | e.printStackTrace();
177 | } finally {
178 | try {
179 | connection.close();
180 | } catch (SQLException e) {
181 | // TODO Auto-generated catch block
182 | LOG.log(Level.ERROR, "PersistentDB - Error " + e.getMessage());
183 | }
184 | }
185 | return maxIDX;
186 | }
187 |
188 |
189 | public ZabbixItem pop() {
190 | //TODO redesign to support Zabbix Proxy label while sending the data to Zabbix. Now it's working...
191 | Connection connection = getDBConnection();
192 | PreparedStatement preparedStmt = null;
193 | ZabbixItem zItem = null;
194 |
195 |
196 | String SelectMaxIDX = "SELECT COALESCE(MAX(IDX),0) as IDX from ITEM";
197 | String SelectMaxQuery = "SELECT IDX,HOST,KEY,VALUE,CLOCK from ITEM where IDX=?";
198 | String deleteIDX = "DELETE FROM ITEM WHERE IDX=?";
199 | try {
200 | connection.setAutoCommit(false);
201 | preparedStmt = connection.prepareStatement(SelectMaxIDX);
202 | ResultSet rs = preparedStmt.executeQuery();
203 | Long maxIDX = 0L;
204 | while (rs.next()) {
205 | maxIDX = rs.getLong("IDX");
206 |
207 | }
208 | rs.close();
209 |
210 | if (maxIDX > 0L ){
211 | preparedStmt = connection.prepareStatement(SelectMaxQuery);
212 | preparedStmt.setLong(1, maxIDX);
213 | rs = preparedStmt.executeQuery();
214 | rs.next();
215 |
216 | zItem= new ZabbixItem(rs.getString("HOST"),
217 | rs.getString("KEY"),
218 | rs.getString("VALUE"),
219 | rs.getLong("CLOCK"));
220 | LOG.log(Level.DEBUG, "PersistentDB - Last Item in database"+
221 | " Host="+ zItem.getHost()+
222 | " Key="+ zItem.getKey()+
223 | " Value="+ zItem.getValue()+
224 | " Clock="+ zItem.getClock().toString());
225 | rs.close();
226 | preparedStmt = connection.prepareStatement(deleteIDX);
227 | preparedStmt.setLong(1, maxIDX);
228 | preparedStmt.executeUpdate();
229 |
230 | }
231 |
232 | preparedStmt.close();
233 | connection.commit();
234 | connection.close();
235 | } catch (SQLException e) {
236 | LOG.log(Level.ERROR, "PersistentDB - Error " + e.getLocalizedMessage());
237 | } catch (Exception e) {
238 | LOG.log(Level.ERROR, "PersistentDB - Error " + e.getMessage());
239 | } finally {
240 | try {
241 | connection.close();
242 | } catch (SQLException e) {
243 | // TODO Auto-generated catch block
244 | LOG.log(Level.ERROR, "PersistentDB - Error " + e.getMessage());
245 | }
246 | }
247 | return zItem;
248 | }
249 |
250 |
251 |
252 |
253 | private static Connection getDBConnection() {
254 | Connection dbConnection = null;
255 | try {
256 | Class.forName(DB_DRIVER);
257 | } catch (ClassNotFoundException e) {
258 | System.out.println(e.getMessage());
259 | }
260 | try {
261 | dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
262 | DB_PASSWORD);
263 | return dbConnection;
264 | } catch (SQLException e) {
265 | LOG.log(Level.ERROR, "PersistentDB - Error " + e.getMessage());
266 | }
267 | return dbConnection;
268 | }
269 |
270 |
271 |
272 |
273 |
274 | public static void main(String[] args) throws Exception {
275 | try {
276 | System.out.println("Size ="+PersistentDB.getInstance().size());
277 | // ZabbixItem zi= new ZabbixItem ("host","key","value",0L);
278 | // PersistentDB.getInstance().push(zi);
279 | // zi= new ZabbixItem ("host1","key1","value1",1L);
280 | // PersistentDB.getInstance().push(zi);
281 | // zi= new ZabbixItem ("host2","key2","value2",2L);
282 | // PersistentDB.getInstance().push(zi);
283 | // zi= new ZabbixItem ("host3","key3","value3",3L);
284 | // PersistentDB.getInstance().push(zi);
285 | // System.out.println("Size ="+PersistentDB.getInstance().size());
286 | // while (PersistentDB.getInstance().size() >0 ){
287 | // zi=PersistentDB.getInstance().pop();
288 | // System.out.println(" Host="+zi.getHost()+" Key="+zi.getKey()+" Value="+zi.getValue()+" Clock="+zi.getClock());
289 | // }
290 |
291 |
292 | } catch (Exception e) {
293 | e.printStackTrace();
294 | }
295 | }
296 |
297 |
298 |
299 | public void add(Collection zItems) {
300 | for(ZabbixItem zItem:zItems){
301 | push(zItem);
302 | }
303 | }
304 |
305 | }
306 |
307 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/common/StackSingletonPersistent.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.common;
2 |
3 |
4 | import java.io.File;
5 | import java.io.FileInputStream;
6 | import java.io.FileNotFoundException;
7 | import java.io.FileOutputStream;
8 | import java.io.FilenameFilter;
9 | import java.io.IOException;
10 | import java.io.ObjectInputStream;
11 | import java.io.ObjectOutputStream;
12 |
13 | import org.apache.log4j.Level;
14 | import org.apache.log4j.Logger;
15 |
16 | import com.smartmarmot.dbforbix.config.Config;
17 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
18 | import com.smartmarmot.dbforbix.zabbix.ZabbixSender;
19 |
20 | public class StackSingletonPersistent implements Persistence{
21 |
22 | private String DIRECTORY = Config.getInstance().getSPDir();
23 | private static final Logger LOG = Logger.getLogger(ZabbixSender.class);
24 | private static StackSingletonPersistent instance;
25 | private Long size;
26 |
27 | private StackSingletonPersistent() {};
28 |
29 | private void init() {
30 | size = 0L;
31 | Config config = Config.getInstance();
32 | if (DIRECTORY.startsWith("./"))
33 | DIRECTORY = DIRECTORY.replaceFirst(".", config.getBasedir());
34 | DIRECTORY.replace("//","/");
35 |
36 | File file = new File(DIRECTORY);
37 | if (!file.exists()) {
38 | LOG.log(Level.ERROR, "Persistence directory "+DIRECTORY +" does not exists");
39 | boolean success = (new File(DIRECTORY)).mkdirs();
40 | if (!success) {
41 | LOG.log(Level.INFO, "Persistence directory "+DIRECTORY +" created");
42 | } else{
43 | LOG.log(Level.ERROR, "Persistence directory "+DIRECTORY +" creation failed");
44 | }
45 | }
46 | File[] files = file.listFiles(new FilenameFilter() {
47 | public boolean accept(File dir, String name) {
48 | return name.endsWith(".ssp");
49 | }
50 | });
51 |
52 | for (int i = 0; i < files.length; i++) {
53 | try {
54 | read(getFile(i+1L));
55 | size++;
56 | } catch (Exception e) {
57 | LOG.log(Level.ERROR, "Persistence directory "+DIRECTORY +" problem "+e);
58 | break;
59 | }
60 | }
61 | }
62 |
63 | private File getFile(Long index) {
64 | return new File(DIRECTORY + "/Item" + index + ".ssp");
65 | }
66 |
67 | private Object read(File file) throws FileNotFoundException, IOException, ClassNotFoundException {
68 | ObjectInputStream itemStream = null;
69 | try {
70 | itemStream = new ObjectInputStream(new FileInputStream(file));
71 | return itemStream.readObject();
72 | } finally {
73 | if (itemStream != null) {
74 | itemStream.close();
75 | }
76 | }
77 | }
78 |
79 | private void save(Object item) throws FileNotFoundException, IOException {
80 | ObjectOutputStream itemStream = null;
81 | try {
82 | itemStream = new ObjectOutputStream(new FileOutputStream(getFile(size)));
83 | itemStream.writeObject(item);
84 | } finally {
85 | if (itemStream != null) {
86 | itemStream.close();
87 | }
88 | }
89 | }
90 |
91 | public static synchronized StackSingletonPersistent getInstance() {
92 | if (instance == null) {
93 | instance = new StackSingletonPersistent();
94 | instance.init();
95 | }
96 | return instance;
97 | }
98 |
99 | public void push(ZabbixItem item) {
100 | synchronized (StackSingletonPersistent.class) {
101 | size++;
102 | try {
103 | save(item);
104 | } catch (Exception e) {
105 | LOG.log(Level.ERROR, "Persistence error:"+ e.getMessage());
106 | }
107 | }
108 | }
109 |
110 | public ZabbixItem pop() {
111 | synchronized (StackSingletonPersistent.class) {
112 | if (size > 0) {
113 | File file = getFile(size);
114 | Object result=null;
115 | try {
116 | result = read(file);
117 | } catch (Exception e) {
118 | e.printStackTrace();
119 | }
120 | file.delete();
121 | size--;
122 | return (ZabbixItem) result;
123 | } else {
124 | return null;
125 | }
126 | }
127 | }
128 |
129 | public Object peek() throws FileNotFoundException, IOException, ClassNotFoundException {
130 | synchronized (StackSingletonPersistent.class) {
131 | if (size > 0L) {
132 | return read(getFile(size));
133 | } else {
134 | return null;
135 | }
136 | }
137 | }
138 |
139 | public Long size() {
140 | synchronized (StackSingletonPersistent.class) {
141 | return size;
142 | }
143 | }
144 |
145 | // public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
146 |
147 | // System.out.println(StackSingletonPersistent.getInstance().size());
148 |
149 | // // Populate
150 |
151 | // StackSingletonPersistent.getInstance().push(new ZabbixItem("chiave","Valore","host"));
152 | // StackSingletonPersistent.getInstance().push(new ZabbixItem("chiave1","Valore1","host1"));
153 | // StackSingletonPersistent.getInstance().push(new ZabbixItem("chiave2","Valore2","host2"));
154 | // StackSingletonPersistent.getInstance().push(new ZabbixItem("chiave3","Valore3","host3"));
155 |
156 | // StackSingletonPersistent.getInstance().push("dhgfjs jhsf jh fsjd");
157 | // StackSingletonPersistent.getInstance().push(new BigDecimal(15898604));
158 | // StackSingletonPersistent.getInstance().push("asdsasdasdas");
159 |
160 | //
161 | // System.out.println(StackSingletonPersistent.getInstance().size());
162 | // System.out.println(StackSingletonPersistent.getInstance().peek());
163 | // while (StackSingletonPersistent.getInstance().size != 0 ){
164 | // ZabbixItem zx = (ZabbixItem) StackSingletonPersistent.getInstance().pop();
165 | // System.out.println("key-->"+zx.getKey()+"Value-->"+zx.getValue()+"Host-->"+zx.getHost());
166 | // }
167 |
168 | // }
169 | }
170 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/common/utils/DBforBIXHelper.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.common.utils;
2 |
3 | import org.apache.log4j.Logger;
4 |
5 | import com.smartmarmot.dbforbix.config.ZabbixServer;
6 |
7 | public final class DBforBIXHelper {
8 |
9 | private static final Logger LOG = Logger.getLogger(DBforBIXHelper.class);
10 |
11 | public static boolean isMacro(String macro) {
12 | String macroMask="^\\{\\$[a-zA-Z0-9_-]+\\}$";
13 | return macro.matches(macroMask);
14 | }
15 |
16 | public static String substituteMacros(String inStr, ZabbixServer zabbixServer, String hostid) {
17 | String result=new String(inStr);
18 | try{// substitute macro
19 | int iStart=0;
20 | int iEnd=0;
21 | iStart=result.indexOf("{$");
22 | while (-1!=iStart){
23 | iEnd=result.indexOf('}', iStart);
24 | if(-1!=iEnd){
25 | String macro=result.substring(iStart, ++iEnd);
26 | if(isMacro(macro)){
27 | String macroValue=zabbixServer.getMacroValue(hostid,macro);
28 | if(null!=macroValue){
29 | result=result.replace(macro, macroValue);
30 | iEnd=iEnd-macro.length()+macroValue.length();
31 | }
32 | }
33 | } else break;
34 | iStart=result.indexOf("{$",iEnd);
35 | }
36 | }
37 | catch (Exception ex){
38 | LOG.error("Error substituting macros for Zabbix Server "+zabbixServer+",hostid = "+hostid+". String: "+ inStr + "\nException:\n" + ex.getLocalizedMessage());
39 | }
40 | return result;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/common/utils/SAXParserDBforBIX.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.common.utils;
2 |
3 | import java.io.InputStream;
4 | import java.io.UnsupportedEncodingException;
5 | import java.nio.charset.StandardCharsets;
6 | import java.io.ByteArrayInputStream;
7 | import java.io.IOException;
8 | import java.util.HashMap;
9 | import java.util.Map;
10 | import javax.xml.parsers.ParserConfigurationException;
11 | import javax.xml.parsers.SAXParser;
12 | import javax.xml.parsers.SAXParserFactory;
13 | import org.xml.sax.SAXException;
14 | import org.xml.sax.SAXParseException;
15 | import org.xml.sax.helpers.DefaultHandler;
16 |
17 |
18 |
19 | public class SAXParserDBforBIX {
20 | //max iterations
21 | private static final int maxIterations=1000;
22 |
23 | //special characters/strings to replace
24 | @SuppressWarnings("serial")
25 | private static final Map mStringToReplace =new HashMap() {{
26 | put(">", ">");
27 | put("<", "<");
28 | }};
29 |
30 | /**
31 | * Parses input XML string trying to fix "<" or ">" signs. Iterates maximum 1000 times and then exits.
32 | * Throws XMLSyntaxUnrecoverableException
33 | * @param inputXMLString
34 | * @return
35 | * @throws XMLDBforBIXUnrecoverableException
36 | */
37 | public static String replaceSpecialChars(String inputXMLString)throws XMLDBforBIXUnrecoverableException{
38 | String result=inputXMLString;
39 | SAXParser parser = null;
40 | InputStream inputStream = null;
41 | SAXParserFactory parserFactor = SAXParserFactory.newInstance();
42 | SAXHandler handler = new SAXHandler();
43 |
44 | try {
45 | parser = parserFactor.newSAXParser();
46 |
47 | } catch (ParserConfigurationException|SAXException e) {
48 | e.printStackTrace();
49 | throw new XMLDBforBIXUnrecoverableException("Unhandled exception came from SAX XML parser.");
50 | }
51 |
52 | int i=0;
53 | for(;i++maxIterations)
77 | throw new XMLDBforBIXUnrecoverableException("Found more than "+maxIterations+" '>', '<' signs (outside of tags!) in XML. Please change them to > or < manually or contact developer.");
78 |
79 | return result;
80 | }
81 |
82 | private static String correctSymbols(String inputString, int lineNumber, int columnNumber) throws XMLDBforBIXUnrecoverableException{
83 | String result=inputString;
84 | boolean specialCharsNotFound=true;
85 |
86 | //find starting position of lineNumber
87 | int fromIndex=0;
88 | for(int i=0; i++ configurationUIDs=new HashSet();
26 |
27 | public DBType getType() {
28 | return type;
29 | }
30 |
31 | public String getDBNameFC() {
32 | return nameFC;
33 | }
34 |
35 | public void setDBNameFC(String nameFC) {
36 | this.nameFC=nameFC;
37 | }
38 |
39 | public String getURL() {
40 | return url;
41 | }
42 |
43 |
44 | public String getUser() {
45 | return user;
46 | }
47 |
48 | public String getPassword() {
49 | return password;
50 | }
51 |
52 | public String getInstance() {
53 | return instance;
54 | }
55 |
56 | @Override
57 | public boolean isValid() {
58 | return (nameFC != null) && (url != null) && (user != null) && (password != null);
59 | }
60 |
61 | @Override
62 | public String toString() {
63 | return "DSN:"+this.getDBNameFC()+",\ttype:"+getType() + ",\tURL:" + getURL() + ",\tInstance:" + getInstance();
64 | }
65 |
66 | public Integer getMaxWaitMillis() {
67 | return maxwaitmillis;
68 | }
69 |
70 | public void setMaxWaitMillis(Integer maxwaitmillis) {
71 | this.maxwaitmillis = maxwaitmillis;
72 | }
73 |
74 | public Integer getMaxIdle() {
75 | return maxidle;
76 | }
77 |
78 | public void setMaxIdle(Integer maxidle) {
79 | this.maxidle = maxidle;
80 | }
81 |
82 |
83 | public void setPersistence(String pers) {
84 | if (pers.equalsIgnoreCase("true")) {
85 | this.persistent = true;
86 | }
87 | }
88 | public Boolean getPersistence(){
89 | return this.persistent;
90 | }
91 |
92 | public void setMaxActive(Integer maxactive) {
93 | this.maxactive= maxactive;
94 | }
95 |
96 | public Integer getMaxActive() {
97 | return maxactive;
98 | }
99 | public int getQueryTimeout() {
100 | return queryTimeout;
101 | }
102 |
103 | public void setQueryTimeout(int queryTimeout) {
104 | this.queryTimeout = queryTimeout;
105 | }
106 |
107 | public Set getConfigurationUIDs() {
108 | return configurationUIDs;
109 | }
110 |
111 | public void addConfigurationUID(String configurationUID) {
112 | configurationUIDs.add(configurationUID);
113 | }
114 |
115 | public void removeConfigurationUID(String configurationUID) {
116 | configurationUIDs.remove(configurationUID);
117 | }
118 |
119 | }
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/ZabbixServer.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config;
2 |
3 | import java.util.Collection;
4 | import java.util.HashMap;
5 | import java.util.HashSet;
6 | import java.util.List;
7 | import java.util.Map;
8 | import java.util.Set;
9 |
10 | import com.smartmarmot.dbforbix.config.Config.Validable;
11 | import com.smartmarmot.dbforbix.config.item.IConfigurationItem;
12 | import com.smartmarmot.dbforbix.zabbix.ZabbixSender.PROTOCOL;
13 |
14 | /**
15 | * Zabbix server config entry
16 | */
17 | public class ZabbixServer implements Validable {
18 |
19 | String zbxServerHost = null;
20 | int zbxServerPort = 10051;
21 | private String zbxServerNameFC = null;
22 | String proxy = null;
23 | private PROTOCOL protocol = PROTOCOL.V32;
24 | private Collection definedDBNames = null;
25 |
26 |
27 | /**
28 | * reinit
29 | */
30 | private Map > hosts=null;
31 | private Map > items=null;
32 | private Map > hostmacro=null;
33 | private Map> hostsTemplates=null;
34 | private Map configurationItems = new HashMap<>();
35 | private String hashZabbixConfig =null;
36 | String zabbixConfigurationItemSuffix = "DBforBIX.config";
37 |
38 |
39 |
40 | /////////////////////////////////////////////////////////
41 | public String getZbxServerNameFC() {
42 | return zbxServerNameFC;
43 | }
44 |
45 | public void setZbxServerNameFC(String zbxServerNameFC) {
46 | this.zbxServerNameFC = zbxServerNameFC;
47 | }
48 |
49 |
50 | public Map getConfigurationItems() {
51 | return configurationItems;
52 | }
53 |
54 | public String getHashZabbixConfig() {
55 | return hashZabbixConfig;
56 | }
57 |
58 | public Map > getHosts() {
59 | return hosts;
60 | }
61 |
62 | public Map > getItems() {
63 | return items;
64 | }
65 |
66 | public Map > getHostmacro() {
67 | return hostmacro;
68 | }
69 | ////////////////////////
70 | public void setHashZabbixConfig(String inStr) {
71 | this.hashZabbixConfig=inStr;
72 | }
73 |
74 |
75 | public void setConfigurationItems(Map configurationItems) {
76 | this.configurationItems=configurationItems;
77 | }
78 |
79 | public void setHosts(Map > hosts) {
80 | this.hosts = hosts;
81 | }
82 |
83 | public void setItems(Map > items) {
84 | this.items = items;
85 | }
86 |
87 | public void setHostmacro(Map > hostmacro) {
88 | this.hostmacro = hostmacro;
89 | }
90 | //////////////////////////////////////////////////////////////////
91 |
92 |
93 | public Collection getDefinedDBNames() {
94 | return definedDBNames;
95 | }
96 |
97 | public void setDefinedDBNames(Collection definedDBNames) {
98 | this.definedDBNames = definedDBNames;
99 | }
100 |
101 | public String getZServerHost() {
102 | return zbxServerHost;
103 | }
104 |
105 | public int getZServerPort() {
106 | return zbxServerPort;
107 | }
108 |
109 | public PROTOCOL getProtocol() {
110 | return protocol;
111 | }
112 |
113 | @Override
114 | public boolean isValid() {
115 | return (zbxServerPort > 0) && (zbxServerHost != null);
116 | }
117 |
118 | @Override
119 | public String toString() {
120 | return zbxServerHost + ":" + zbxServerPort;
121 | }
122 |
123 |
124 | public String getProxyConfigRequest(){
125 | return new String("{\"request\":\"proxy config\",\"host\":\""+getProxy()+"\"}");
126 | }
127 |
128 | public String getProxy() {
129 | return proxy;
130 | }
131 |
132 | public void setProxy(String proxy) {
133 | this.proxy = proxy;
134 | }
135 |
136 | public void addConfigurationItem(IConfigurationItem configurationItem) {
137 | configurationItems.put(configurationItem.getConfigurationUID(), configurationItem);
138 | }
139 |
140 | public void removeConfigurationItem(String configurationUID) {
141 | configurationItems.remove(configurationUID);
142 | }
143 |
144 | /**
145 | *
146 | * @return set of item group names of this Zabbix Server
147 | */
148 | public Collection getConfigurationUIDs() {
149 | return configurationItems.keySet();
150 | }
151 |
152 | /**
153 | *
154 | * @param configurationUID
155 | * @return configuration item
156 | * @throws NullPointerException
157 | */
158 | public IConfigurationItem getConfigurationItemByConfigurationUID(String configurationUID){
159 | return configurationItems.get(configurationUID);
160 | }
161 |
162 |
163 |
164 | public String getHostByHostId(String hostid) {
165 | String host=null;
166 | for(int hid=0;hid templateIds=new HashSet<>();
196 | for(int hm=0;hm> hostsTemplates) {
212 | this.hostsTemplates=hostsTemplates;
213 | }
214 |
215 | public Map> getHostsTemplates() {
216 | return hostsTemplates;
217 | }
218 |
219 | public String getMacroValue(String hostid, String macro) {
220 | String result=null;
221 | result=getHostMacroValue(hostid,macro);
222 | if(null==result){
223 | result=this.getTemplateMacroValue(hostid, macro);
224 | }
225 | return result;
226 | }
227 |
228 | public String getConfigurationItemSuffix() {
229 | return zabbixConfigurationItemSuffix;
230 | }
231 | }
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/element/AbstractConfigurationElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.config.element;
19 |
20 | import com.smartmarmot.dbforbix.config.ZabbixServer;
21 | import com.smartmarmot.dbforbix.config.item.IConfigurationItem;
22 |
23 | abstract class AbstractConfigurationElement implements IConfigurationElement {
24 |
25 | private String id;
26 | private String prefix;
27 | private String query;
28 | private int time=60;
29 | private String noData=null;
30 | private IConfigurationItem configurationItem=null;
31 |
32 |
33 | public AbstractConfigurationElement(String _prefix, int _time, String _item_or_items, String _noData, String _query){
34 | prefix = _prefix;
35 | time=_time>0?_time:time;
36 | id = _prefix+_item_or_items;
37 | noData = _noData;
38 | query = _query;
39 | }
40 |
41 | @Override
42 | public int getTime(){
43 | return time;
44 | }
45 |
46 | @Override
47 | public ZabbixServer getZabbixServer(){
48 | return configurationItem.getZabbixServer();
49 | }
50 |
51 | @Override
52 | public String getElementID() {
53 | return id;
54 | }
55 |
56 | /**
57 | * @return the prefix
58 | */
59 | @Override
60 | public String getPrefix() {
61 | return prefix;
62 | }
63 |
64 | /**
65 | * @return the query
66 | */
67 | @Override
68 | public String getQuery() {
69 | return query;
70 | }
71 |
72 | @Override
73 | public String getNoData(){
74 | return noData;
75 | }
76 |
77 | @Override
78 | public void setConfigurationItem(IConfigurationItem _configurationItem){
79 | configurationItem=_configurationItem;
80 | }
81 |
82 | @Override
83 | public IConfigurationItem getConfigurationItem() {
84 | return configurationItem;
85 | }
86 |
87 | @Override
88 | public String getConfigurationUID() throws NullPointerException{
89 | return this.getConfigurationItem().getConfigurationUID();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/element/AbstractMultiConfigurationElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.config.element;
19 |
20 | abstract class AbstractMultiConfigurationElement extends AbstractConfigurationElement {
21 |
22 | protected String[] itemKeys;
23 |
24 | AbstractMultiConfigurationElement(String _prefix, int _time, String _items, String _noData, String _query) {
25 | super(_prefix, _time, _items, _noData, _query);
26 | itemKeys = _items.split("\\|");
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/element/ConfigurationElementFactory.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config.element;
2 |
3 | import java.security.InvalidParameterException;
4 |
5 | public class ConfigurationElementFactory {
6 |
7 | public static IConfigurationElement buildConfigurationElement(String _configurationElementType,
8 | String _prefix, int _time, String _item_or_items, String _names, String _noData, String _type, String _query)
9 | throws InvalidParameterException {
10 |
11 | IConfigurationElement configurationElement = null;
12 | switch (_configurationElementType) {
13 | case "discovery": {
14 | configurationElement = new DiscoveryConfigurationElement(_prefix, _time, _item_or_items, _names, _query);
15 | }
16 | break;
17 | case "query": {
18 | configurationElement = new SimpleConfigurationElement(_prefix,_time,_item_or_items,_noData,_query);
19 | }
20 | break;
21 | case "multiquery": {
22 | if (_type.equalsIgnoreCase("column"))
23 | configurationElement = new MultiColumnConfigurationElement(_prefix, _time, _item_or_items, _noData, _query);
24 | else
25 | configurationElement = new MultiRowConfigurationElement(_prefix, _time, _item_or_items, _noData, _query);
26 | }
27 | break;
28 | default: throw new InvalidParameterException("Unknown configuration element type: "+_configurationElementType);
29 | }
30 | return configurationElement;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/element/DiscoveryConfigurationElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.config.element;
19 |
20 | import java.sql.Connection;
21 | import java.sql.PreparedStatement;
22 | import java.sql.ResultSet;
23 | import java.sql.ResultSetMetaData;
24 | import java.sql.SQLException;
25 | import org.apache.log4j.Logger;
26 |
27 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
28 |
29 | public class DiscoveryConfigurationElement extends AbstractConfigurationElement {
30 |
31 | private static final Logger LOG = Logger.getLogger(DiscoveryConfigurationElement.class);
32 | private String discoveryItemKey;
33 | private String[] altNames;
34 |
35 | public DiscoveryConfigurationElement(String _prefix, int time, String _item, String _names, String _query) {
36 | super(_prefix, time, _item, "", _query);
37 | discoveryItemKey = _prefix+_item;
38 | altNames = _names.split("\\|");
39 | }
40 |
41 | @Override
42 | public ZabbixItem[] getZabbixItemsData(Connection con, int timeout) throws SQLException {
43 | StringBuilder builder = new StringBuilder();
44 | boolean first = true;
45 | Long clock = new Long(System.currentTimeMillis() / 1000L);
46 |
47 | try(PreparedStatement pstmt = con.prepareStatement(getQuery())){
48 | pstmt.setQueryTimeout(timeout);
49 | try(ResultSet rs = pstmt.executeQuery()){
50 | ResultSetMetaData meta = rs.getMetaData();
51 |
52 | builder.append("{\"data\":[");
53 | while (rs.next()) {
54 | if (first)
55 | builder.append("{");
56 | else
57 | builder.append(",{");
58 | for (int i = 1; i <= meta.getColumnCount(); i++) {
59 |
60 | /**
61 | * {"request":"agent data","data":[
62 | * {"host":"zabdomis01","key":"net.if.discovery","value":"{\"data\":[{\"{#IFNAME}\":\"lo\"},{\"{#IFNAME}\":\"eno16780032\"}]}","clock":1481573917,"ns":417446137},
63 | * {"host":"zabdomis01","key":"vfs.fs.discovery","value":"{\"data\":[
64 | * {\"{#FSNAME}\":\"/\",\"{#FSTYPE}\":\"rootfs\"},{\"{#FSNAME}\":\"/sys\",\"{#FSTYPE}\":\"sysfs\"},{\"{#FSNAME}\":\"/proc\",\"{#FSTYPE}\":\"proc\"},{\"{#FSNAME}\":\"/dev\",\"{#FSTYPE}\":\"devtmpfs\"},{\"{#FSNAME}\":\"/sys/kernel/security\",\"{#FSTYPE}\":\"securityfs\"},
65 | * {\"{#FSNAME}\":\"/dev/shm\",\"{#FSTYPE}\":\"tmpfs\"},{\"{#FSNAME}\":\"/dev/pts\",\"{#FSTYPE}\":\"devpts\"},{\"{#FSNAME}\":\"/run\",\"{#FSTYPE}\":\"tmpfs\"},{\"{#FSNAME}\":\"/sys/fs/cgroup\",\"{#FSTYPE}\":\"tmpfs\"},
66 | * {\"{#FSNAME}\":\"/sys/fs/cgroup/systemd\",\"{#FSTYPE}\":\"cgroup\"},{\"{#FSNAME}\":\"/sys/fs/pstore\",\"{#FSTYPE}\":\"pstore\"},
67 | * {\"{#FSNAME}\":\"/sys/fs/cgroup/cpu,cpuacct\",\"{#FSTYPE}\":\"cgroup\"},{\"{#FSNAME}\":\"/sys/fs/cgroup/devices\",\"{#FSTYPE}\":\"cgroup\"},
68 | * {\"{#FSNAME}\":\"/sys/fs/cgroup/perf_event\",\"{#FSTYPE}\":\"cgroup\"},{\"{#FSNAME}\":\"/sys/fs/cgroup/hugetlb\",\"{#FSTYPE}\":\"cgroup\"}, * {\"{#FSNAME}\":\"/sys/fs/cgroup/cpuset\",\"{#FSTYPE}\":\"cgroup\"},{\"{#FSNAME}\":\"/sys/fs/cgroup/net_cls\",\"{#FSTYPE}\":\"cgroup\"},
69 | * {\"{#FSNAME}\":\"/sys/fs/cgroup/memory\",\"{#FSTYPE}\":\"cgroup\"},
70 | * {\"{#FSNAME}\":\"/sys/fs/cgroup/freezer\",\"{#FSTYPE}\":\"cgroup\"},
71 | * {\"{#FSNAME}\":\"/sys/fs/cgroup/blkio\",\"{#FSTYPE}\":\"cgroup\"},
72 | * {\"{#FSNAME}\":\"/sys/kernel/config\",\"{#FSTYPE}\":\"configfs\"},
73 | * {\"{#FSNAME}\":\"/\",\"{#FSTYPE}\":\"xfs\"},
74 | * {\"{#FSNAME}\":\"/proc/sys/fs/binfmt_misc\",\"{#FSTYPE}\":\"autofs\"}, * {\"{#FSNAME}\":\"/dev/hugepages\",\"{#FSTYPE}\":\"hugetlbfs\"}, * {\"{#FSNAME}\":\"/sys/kernel/debug\",\"{#FSTYPE}\":\"debugfs\"},
75 | * {\"{#FSNAME}\":\"/dev/mqueue\",\"{#FSTYPE}\":\"mqueue\"}, * {\"{#FSNAME}\":\"/proc/sys/fs/binfmt_misc\",\"{#FSTYPE}\":\"binfmt_misc\"},
76 | * {\"{#FSNAME}\":\"/proc/fs/nfsd\",\"{#FSTYPE}\":\"nfsd\"}, * {\"{#FSNAME}\":\"/var\",\"{#FSTYPE}\":\"xfs\"},{\"{#FSNAME}\":\"/var/lib/nfs/rpc_pipefs\",\"{#FSTYPE}\":\"rpc_pipefs\"},{\"{#FSNAME}\":\"/home\",\"{#FSTYPE}\":\"xfs\"},{\"{#FSNAME}\":\"/opt\",\"{#FSTYPE}\":\"xfs\"},{\"{#FSNAME}\":\"/tmp\",\"{#FSTYPE}\":\"xfs\"},{\"{#FSNAME}\":\"/boot\",\"{#FSTYPE}\":\"xfs\"},{\"{#FSNAME}\":\"/app\",\"{#FSTYPE}\":\"xfs\"},{\"{#FSNAME}\":\"/misc\",\"{#FSTYPE}\":\"autofs\"},{\"{#FSNAME}\":\"/net\",\"{#FSTYPE}\":\"autofs\"},{\"{#FSNAME}\":\"/run/user/354789\",\"{#FSTYPE}\":\"tmpfs\"},{\"{#FSNAME}\":\"/run/user/0\",\"{#FSTYPE}\":\"tmpfs\"},
77 | * {\"{#FSNAME}\":\"/run/user/59425\",\"{#FSTYPE}\":\"tmpfs\"},
78 | * {\"{#FSNAME}\":\"/run/user/361033\",\"{#FSTYPE}\":\"tmpfs\"}]
79 | * }",
80 | * "clock":1481573917,"ns":417779254}],
81 | * "clock":1481573922,"ns":418957607}
82 | */
83 |
84 | /**
85 | * comma for separate names inside one discovery block
86 | */
87 | if(i>1) builder.append(",");
88 | if (altNames == null)
89 | builder.append("\"{#" + meta.getColumnName(i).toUpperCase() + "}\":\"" + rs.getString(i).replace("\\", "\\\\") +
90 | "\"");
91 | else
92 | builder.append("\"{#" + altNames[i-1].toUpperCase() + "}\":\"" + rs.getString(i).replace("\\", "\\\\") +
93 | "\"");
94 | }
95 | /**
96 | * close discovery block
97 | */
98 | builder.append("}");
99 | /**
100 | * first discovery block has been passed
101 | */
102 | first = false;
103 | }
104 | }catch(SQLException ex){
105 | throw ex;
106 | }
107 | }catch(SQLException ex){
108 | LOG.error("Cannot get data for item:\n" + getElementID() +"\nQuery:\n"+getQuery(), ex);
109 | throw ex;
110 | }
111 |
112 | builder.append("]}");
113 | return new ZabbixItem[] { new ZabbixItem(discoveryItemKey, builder.toString(),ZabbixItem.ZBX_STATE_NORMAL,clock, this) };
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/element/IConfigurationElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.config.element;
19 |
20 | import java.sql.Connection;
21 | import java.sql.SQLException;
22 |
23 | import com.smartmarmot.dbforbix.config.ZabbixServer;
24 | import com.smartmarmot.dbforbix.config.item.IConfigurationItem;
25 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
26 |
27 | /**
28 | * Configuration element is part of configuration item.
29 | * One configuration item can consists of several configuration elements.
30 | * ConfigurationItem : configurationElement -> 1:N
31 | */
32 | public interface IConfigurationElement {
33 | String getElementID();
34 | String getPrefix();
35 | ZabbixItem[] getZabbixItemsData(Connection con, int timeout) throws SQLException;
36 | ZabbixServer getZabbixServer();
37 | IConfigurationItem getConfigurationItem();
38 | void setConfigurationItem(IConfigurationItem configurationItem);
39 | String getQuery();
40 | String getNoData();
41 | int getTime();
42 | String getConfigurationUID() throws NullPointerException;
43 | }
44 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/element/MultiColumnConfigurationElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.config.element;
19 |
20 | import java.sql.Connection;
21 | import java.sql.PreparedStatement;
22 | import java.sql.ResultSet;
23 | import java.sql.ResultSetMetaData;
24 | import java.sql.SQLException;
25 | import java.util.ArrayList;
26 | import java.util.List;
27 | import org.apache.log4j.Logger;
28 |
29 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
30 |
31 |
32 | public class MultiColumnConfigurationElement extends AbstractMultiConfigurationElement {
33 |
34 | private static final Logger LOG = Logger.getLogger(MultiColumnConfigurationElement.class);
35 |
36 | public MultiColumnConfigurationElement(String _prefix, int _time, String _items, String _noData, String _query) {
37 | super(_prefix, _time, _items, _noData, _query);
38 | }
39 |
40 | @Override
41 | public ZabbixItem[] getZabbixItemsData(Connection con, int timeout) throws SQLException {
42 | Long clock = new Long(System.currentTimeMillis() / 1000L);
43 | List values = new ArrayList<>();
44 | /**
45 | * Statement has to be closed or "Error message: ORA-01000: maximum open cursors exceeded" occurs with time otherwise
46 | */
47 | try(PreparedStatement pstmt = con.prepareStatement(getQuery())){
48 | pstmt.setQueryTimeout(timeout);
49 | try(ResultSet rs = pstmt.executeQuery()){
50 |
51 | /**
52 | * Example:
53 | *
54 | * SELECT table_schema "database", SUM(index_length) "size", SUM(data_free) "free"
55 | * FROM INFORMATION_SCHEMA.TABLES
56 | * WHERE table_schema NOT IN ('information_schema','performance_schema') GROUP BY table_schema
57 | *
58 | */
59 |
60 | while (rs.next()) {//it can be multirows
61 | ResultSetMetaData meta = rs.getMetaData();
62 | if (meta.getColumnCount() < itemKeys.length) {
63 | LOG.error("Number of columns in SQL select of configuration element "+getElementID()+"\nof configuration item: "+getConfigurationItem().getConfigurationUID()+
64 | "\nis less than in configuration item");
65 | break;
66 | }
67 | else {
68 | //from the last column to first
69 | for (int it=itemKeys.length-1, column=meta.getColumnCount();it>=0;--it,--column){
70 | //name[%1_%2_%5] -> name[one_two_three]
71 | String realName = itemKeys[it];
72 | for(int i = 1; i<= meta.getColumnCount(); i++)
73 | realName = realName.replace("%"+i, (null != rs.getString(i)) ? rs.getString(i) : getNoData() );
74 | //get value
75 | values.add(new ZabbixItem(getPrefix()+realName,
76 | (null != rs.getString(column)) ? rs.getString(column) : getNoData(),
77 | ZabbixItem.ZBX_STATE_NORMAL,clock, this));
78 | }
79 | }
80 | }
81 | }
82 | /**
83 | * Autoclose rs statement
84 | */
85 | catch(SQLException ex){
86 | throw ex;
87 | }
88 | }catch(SQLException ex){
89 | LOG.error("Cannot get data for configuration element: " + getElementID() +"\nQuery:\n"+getQuery(), ex);
90 | throw ex;
91 | }
92 | return values.toArray(new ZabbixItem[0]);
93 | }
94 |
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/element/MultiRowConfigurationElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.config.element;
19 |
20 | import java.sql.Connection;
21 | import java.sql.PreparedStatement;
22 | import java.sql.ResultSet;
23 | import java.sql.SQLException;
24 | import java.util.HashMap;
25 | import java.util.Map;
26 | import org.apache.log4j.Logger;
27 |
28 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
29 |
30 | public class MultiRowConfigurationElement extends AbstractMultiConfigurationElement {
31 |
32 | private static final Logger LOG = Logger.getLogger(MultiRowConfigurationElement.class);
33 |
34 | public MultiRowConfigurationElement(String _prefix, int _time, String _items, String _noData, String _query) {
35 | //noData shouldn't be null!!! Initialize by empty string at least!
36 | super(_prefix, _time, _items, _noData==null?"":_noData, _query);
37 | }
38 |
39 | @Override
40 | public ZabbixItem[] getZabbixItemsData(Connection con, int timeout) throws SQLException {
41 | Long clock = new Long(System.currentTimeMillis() / 1000L);
42 | Map values = new HashMap<>();
43 |
44 | try(PreparedStatement pstmt = con.prepareStatement(getQuery())){
45 | pstmt.setQueryTimeout(timeout);
46 | try(ResultSet rs = pstmt.executeQuery()){
47 |
48 | // fill with base items
49 | for (String itemKey: itemKeys)
50 | values.put(itemKey, new ZabbixItem(getPrefix()+itemKey, getNoData(), ZabbixItem.ZBX_STATE_NORMAL,clock, this));
51 |
52 | // now check if we find better values
53 | while (rs.next()) {
54 | String fetchedName = rs.getString(1).toLowerCase();
55 | String fetchedVal = rs.getString(2);
56 | if (fetchedVal != null && values.containsKey(fetchedName)) {
57 | clock = new Long(System.currentTimeMillis() / 1000L);
58 | values.put(fetchedName, new ZabbixItem(getPrefix()+fetchedName, fetchedVal,ZabbixItem.ZBX_STATE_NORMAL,clock,this));
59 |
60 | }
61 | }
62 | }catch(SQLException ex){
63 | throw ex;
64 | }
65 | }catch(SQLException ex){
66 | LOG.error("Cannot get data for items:\n" + getElementID() +"\nQuery:\n"+getQuery(), ex);
67 | throw ex;
68 | }
69 |
70 | return values.values().toArray(new ZabbixItem[0]);
71 | }
72 |
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/element/SimpleConfigurationElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.config.element;
19 |
20 | import java.sql.Connection;
21 | import java.sql.PreparedStatement;
22 | import java.sql.ResultSet;
23 | import java.sql.ResultSetMetaData;
24 | import java.sql.SQLException;
25 | import java.util.ArrayList;
26 | import java.util.List;
27 | import org.apache.log4j.Logger;
28 |
29 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
30 |
31 |
32 | public class SimpleConfigurationElement extends AbstractConfigurationElement {
33 |
34 | private static final Logger LOG = Logger.getLogger(SimpleConfigurationElement.class);
35 | private String simpleItemKey;
36 |
37 | public SimpleConfigurationElement(String _prefix, int _time, String _item, String _noData, String _query) {
38 | super(_prefix,_time,_item,_noData,_query);
39 | simpleItemKey=_prefix+_item;
40 | }
41 |
42 | @Override
43 | public ZabbixItem[] getZabbixItemsData(Connection con, int timeout) throws SQLException {
44 | List values = new ArrayList();
45 | try(PreparedStatement pstmt = con.prepareStatement(getQuery())){
46 | pstmt.setQueryTimeout(timeout);
47 | try(ResultSet resultSet = pstmt.executeQuery()){
48 | String val = getNoData();
49 | Long clock = new Long(System.currentTimeMillis() / 1000L);
50 |
51 | while (resultSet.next()) {
52 |
53 | /**
54 | * Get item value
55 | */
56 | val=getNoData();
57 | ResultSetMetaData meta = resultSet.getMetaData();
58 | if (meta.getColumnCount() == 1) {
59 | String fetchedVal = resultSet.getString(1);
60 | if (fetchedVal != null)
61 | val = fetchedVal;
62 | }
63 | else {
64 | int colNum = 1;
65 | try {
66 | colNum = resultSet.findColumn("value");
67 | }
68 | catch (SQLException sqlex) {
69 | colNum = meta.getColumnCount(); // to retrieve the last column
70 | }
71 | String fetchedVal = resultSet.getString(colNum);
72 | if (fetchedVal != null)
73 | val = fetchedVal;
74 | }
75 |
76 | /**
77 | * Get item key
78 | */
79 | String realName = simpleItemKey;
80 | for(int i = 1; i<= meta.getColumnCount(); i++){
81 | realName = realName.replace("%"+i, (null != resultSet.getString(i)) ? resultSet.getString(i) : getNoData() );
82 | }
83 |
84 |
85 | values.add(new ZabbixItem(realName, val,ZabbixItem.ZBX_STATE_NORMAL, clock, this));
86 | }
87 | }catch(SQLException ex){
88 | throw ex;
89 | }
90 | }catch(SQLException ex){
91 | LOG.error("Cannot get data for item:\n" + getElementID() +"\nQuery:\n"+getQuery(), ex);
92 | throw ex;
93 | }
94 |
95 |
96 | return values.toArray(new ZabbixItem[0]);
97 | }
98 |
99 |
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/item/ConfigurationItem.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config.item;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 |
6 | import com.smartmarmot.dbforbix.config.ZabbixServer;
7 | import com.smartmarmot.dbforbix.config.element.IConfigurationElement;
8 |
9 | public class ConfigurationItem implements IConfigurationItem {
10 |
11 | private ZabbixServer zabbixServer;
12 | private String configurationUID, host, hostid, db, key, param, hashParam;
13 | private Set configurationElements = new HashSet<>();
14 |
15 | public ConfigurationItem(String configurationUID, ZabbixServer zabbixServer, String host, String hostid, String db, String key, String param,
16 | String hashParam) {
17 | setConfigurationUID(configurationUID);
18 | setZabbixServer(zabbixServer);
19 | setHost(host);
20 | setHostid(hostid);
21 | setDb(db);
22 | setKey(key);
23 | setParam(param);
24 | setHashParam(hashParam);
25 | }
26 | /* (non-Javadoc)
27 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getConfigurationUID()
28 | */
29 | @Override
30 | public String getConfigurationUID() {
31 | return configurationUID;
32 | }
33 | /* (non-Javadoc)
34 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#setConfigurationUID(java.lang.String)
35 | */
36 | @Override
37 | public void setConfigurationUID(String _configurationUID) {
38 | this.configurationUID = _configurationUID;
39 | }
40 | /* (non-Javadoc)
41 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getHost()
42 | */
43 | @Override
44 | public String getHost() {
45 | return host;
46 | }
47 | /* (non-Javadoc)
48 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#setHost(java.lang.String)
49 | */
50 | @Override
51 | public void setHost(String _host) {
52 | this.host = _host;
53 | }
54 | /* (non-Javadoc)
55 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getHostid()
56 | */
57 | @Override
58 | public String getHostid() {
59 | return hostid;
60 | }
61 | /* (non-Javadoc)
62 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#setHostid(java.lang.String)
63 | */
64 | @Override
65 | public void setHostid(String _hostid) {
66 | this.hostid = _hostid;
67 | }
68 | /* (non-Javadoc)
69 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getDb()
70 | */
71 | @Override
72 | public String getDb() {
73 | return db;
74 | }
75 | /* (non-Javadoc)
76 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#setDb(java.lang.String)
77 | */
78 | @Override
79 | public void setDb(String _db) {
80 | db = _db;
81 | }
82 | /* (non-Javadoc)
83 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getKey()
84 | */
85 | @Override
86 | public String getKey() {
87 | return key;
88 | }
89 | /* (non-Javadoc)
90 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#setKey(java.lang.String)
91 | */
92 | @Override
93 | public void setKey(String _key) {
94 | key = _key;
95 | }
96 | /* (non-Javadoc)
97 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getParam()
98 | */
99 | @Override
100 | public String getParam() {
101 | return param;
102 | }
103 | /* (non-Javadoc)
104 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#setParam(java.lang.String)
105 | */
106 | @Override
107 | public void setParam(String _param) {
108 | param = _param;
109 | }
110 | /* (non-Javadoc)
111 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getHashParam()
112 | */
113 | @Override
114 | public String getHashParam() {
115 | return hashParam;
116 | }
117 | /* (non-Javadoc)
118 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#setHashParam(java.lang.String)
119 | */
120 | @Override
121 | public void setHashParam(String _hashParam) {
122 | hashParam = _hashParam;
123 | }
124 | /* (non-Javadoc)
125 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getZabbixServer()
126 | */
127 | @Override
128 | public ZabbixServer getZabbixServer() {
129 | return zabbixServer;
130 | }
131 |
132 | /* (non-Javadoc)
133 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#setZabbixServer(com.smartmarmot.dbforbix.config.Config.ZabbixServer)
134 | */
135 | @Override
136 | public void setZabbixServer(ZabbixServer _zabbixServer) {
137 | zabbixServer = _zabbixServer;
138 | }
139 |
140 |
141 | /**
142 | * Make two way links
143 | */
144 | @Override
145 | public void addConfigurationElements(Set _configurationElements) {
146 | for(IConfigurationElement configurationElement:_configurationElements){
147 | configurationElement.setConfigurationItem(this);
148 | }
149 | configurationElements.addAll(configurationElements);
150 | }
151 |
152 | /* (non-Javadoc)
153 | * @see com.smartmarmot.dbforbix.scheduler.IConfigurationItem#getConfigurationElements()
154 | */
155 | @Override
156 | public Set getConfigurationElements(){
157 | return configurationElements;
158 | }
159 |
160 |
161 |
162 | }
163 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/item/ConfigurationItemParserFactory.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config.item;
2 |
3 | import java.util.regex.Matcher;
4 | import java.util.regex.Pattern;
5 |
6 | /**
7 | * Factory for Zabbix configuration parser
8 | * @author Vagabondan
9 | *
10 | */
11 | public class ConfigurationItemParserFactory {
12 |
13 | public static IConfigurationItemParser getConfigurationItemParser(String config) throws TypeNotPresentException {
14 | IConfigurationItemParser configurationItemParser=null;
15 | ConfigurationItemType zabbixConfigurationType;
16 | Pattern p = Pattern.compile("^\\s*<\\s*parms\\s+.*");
17 | Matcher m = p.matcher(config);
18 | if (m.find()){
19 | zabbixConfigurationType=ConfigurationItemType.XML;
20 | }else{
21 | zabbixConfigurationType=ConfigurationItemType.Native;
22 | }
23 | switch(zabbixConfigurationType){
24 | case XML:
25 | configurationItemParser=new ConfigurationItemParserXML(config);break;
26 | case Native:
27 | configurationItemParser=new ConfigurationItemParserNative(config);break;
28 | default:
29 | throw new TypeNotPresentException("Couldn't recognize configuration item type! :(",null);
30 | }
31 | return configurationItemParser;
32 | }
33 |
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/item/ConfigurationItemParserNative.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config.item;
2 |
3 | import java.util.Set;
4 |
5 | import org.apache.commons.lang3.NotImplementedException;
6 |
7 | import com.smartmarmot.dbforbix.config.element.IConfigurationElement;
8 |
9 | public class ConfigurationItemParserNative implements IConfigurationItemParser {
10 |
11 | public ConfigurationItemParserNative(String config) {
12 | // TODO Auto-generated constructor stub
13 | throw new NotImplementedException("ConfigurationItemParserNative class is not implemented yet!");
14 | }
15 |
16 | @Override
17 | public Set buildConfigurationElements() {
18 | // TODO Auto-generated method stub
19 | return null;
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/item/ConfigurationItemParserXML.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config.item;
2 |
3 | import java.security.InvalidParameterException;
4 | import java.util.Collection;
5 | import java.util.HashSet;
6 | import java.util.Set;
7 |
8 | import org.apache.log4j.Logger;
9 | import org.dom4j.Document;
10 | import org.dom4j.DocumentException;
11 | import org.dom4j.DocumentHelper;
12 | import org.dom4j.Element;
13 |
14 | import com.smartmarmot.common.utils.SAXParserDBforBIX;
15 | import com.smartmarmot.dbforbix.config.element.ConfigurationElementFactory;
16 | import com.smartmarmot.dbforbix.config.element.IConfigurationElement;
17 |
18 | public class ConfigurationItemParserXML implements IConfigurationItemParser {
19 |
20 | private static final Logger LOG = Logger.getLogger(ConfigurationItemParserXML.class);
21 |
22 | private String config;
23 |
24 | public ConfigurationItemParserXML(String _config) {
25 | config=_config;
26 | }
27 |
28 | @Override
29 | public Set buildConfigurationElements() throws DocumentException {
30 | Set configurationElements=new HashSet<>();
31 | try{
32 | //add constant header - DTD
33 | config=""+config;
34 | //substitute special chars >, < for XML DOM parser
35 | config=preprocessZabbixConfig(config);
36 | }catch(Exception e){
37 | LOG.error("Exception while preprocessing XML configuration: "+e.getLocalizedMessage()+"\nBut we are still trying to construct XML DOM...");
38 | }
39 |
40 | Document doc = DocumentHelper.parseText(config);
41 | Element root = doc.getRootElement();
42 | String prefix = root.attributeValue("prefix","");
43 | for (Object xmlServerElement: root.elements("server")) {
44 | if (xmlServerElement instanceof Element)
45 | configurationElements.addAll(buildConfigurationElementsByXMLServer(prefix,(Element) xmlServerElement));
46 | }
47 | return configurationElements;
48 | }
49 |
50 | /**
51 | * Substitute > and < characters on & gt; and & lt; if they are not XML tags. Used to prepare statement for standard JSON parser.
52 | * XML should contain DTD file because we need to know the template and your XML language keywords!
53 | * @param inputString - configuration from Zabbix configuration item
54 | * @return String - preprocessed configuration
55 | */
56 | private String preprocessZabbixConfig(String inputString) {
57 | return SAXParserDBforBIX.replaceSpecialChars(inputString);
58 | }
59 |
60 | private Collection buildConfigurationElementsByXMLServer(String prefix, Element xmlServer) {
61 | Set configurationElements = new HashSet<>();
62 | for (Object xmlConfigurationObject: xmlServer.elements()) {
63 | if (xmlConfigurationObject instanceof Element) {
64 | Element xmlConfigurationElement = (Element) xmlConfigurationObject;
65 | configurationElements.add(buildConfigurationElementFromXML(prefix,xmlConfigurationElement));
66 | }
67 | }
68 | return configurationElements;
69 | }
70 |
71 | /**
72 | * Builds configuration element from its XML representation
73 | * @param prefix
74 | * @param xmlConfigurationElement
75 | * @return
76 | * @throws InvalidParameterException
77 | */
78 | private IConfigurationElement buildConfigurationElementFromXML(String prefix, Element xmlConfigurationElement) throws InvalidParameterException {
79 | String configurationElementType=xmlConfigurationElement.getName();
80 | int time = 60;
81 | try {
82 | time = Integer.parseInt(xmlConfigurationElement.attributeValue("time"));
83 | }
84 | catch (NumberFormatException ex) {
85 | LOG.warn("Invalid time value " + xmlConfigurationElement.attributeValue("time") +
86 | " for configuration element "+xmlConfigurationElement.attributeValue("item")+
87 | "\nSet time to default value 60 seconds");
88 | }
89 | //can be item or items
90 | String item_or_items=xmlConfigurationElement.attributeValue("item","")+
91 | xmlConfigurationElement.attributeValue("items","");
92 | String names=xmlConfigurationElement.attributeValue("names","");
93 | String noData=xmlConfigurationElement.attributeValue("nodata","");
94 | String type=xmlConfigurationElement.attributeValue("type","column");
95 | String query=xmlConfigurationElement.getTextTrim();
96 |
97 | IConfigurationElement configurationElement=ConfigurationElementFactory.buildConfigurationElement(
98 | configurationElementType, prefix, time, item_or_items, names, noData, type, query
99 | );
100 | return configurationElement;
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/item/ConfigurationItemType.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config.item;
2 |
3 | public enum ConfigurationItemType {
4 | XML, Native;
5 | }
6 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/item/IConfigurationItem.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config.item;
2 |
3 | import java.util.Set;
4 |
5 | import com.smartmarmot.dbforbix.config.ZabbixServer;
6 | import com.smartmarmot.dbforbix.config.element.IConfigurationElement;
7 |
8 | public interface IConfigurationItem {
9 |
10 | String getConfigurationUID();
11 | void setConfigurationUID(String _configurationUID);
12 | String getHost();
13 | void setHost(String _host);
14 | String getHostid();
15 | void setHostid(String _hostid);
16 | String getDb();
17 | void setDb(String _db);
18 | String getKey();
19 | void setKey(String _key);
20 | String getParam();
21 | void setParam(String _param);
22 | String getHashParam();
23 | void setHashParam(String _hashParam);
24 | ZabbixServer getZabbixServer();
25 | void setZabbixServer(ZabbixServer _zabbixServer);
26 | Set getConfigurationElements();
27 | void addConfigurationElements(Set configurationElements);
28 |
29 | }
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/config/item/IConfigurationItemParser.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.config.item;
2 |
3 | import java.util.Set;
4 |
5 | import org.dom4j.DocumentException;
6 |
7 | import com.smartmarmot.dbforbix.config.element.IConfigurationElement;
8 |
9 | public interface IConfigurationItemParser {
10 |
11 | Set buildConfigurationElements() throws DocumentException;
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/DBConstants.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.db;
2 |
3 | public class DBConstants {
4 | public static final String ORACLE= "ORACLE";
5 | public static final String ORACLE_VALIDATION_QUERY = "SELECT SYSDATE FROM DUAL";
6 | public static final String ORACLE_DRIVER = "oracle.jdbc.OracleDriver";
7 | public static final String ORACLE_WHOAMI_QUERY = "SELECT SYS_CONTEXT ('USERENV', 'SESSION_USER') FROM DUAL";
8 | public static final String ORACLE_DBNAME_QUERY = "SELECT SYS_CONTEXT ('USERENV', 'DB_NAME') FROM DUAL";
9 |
10 | public static final String MYSQL="MYSQL";
11 | public static final String MYSQL_VALIDATION_QUERY = "SELECT 1 FROM DUAL";
12 | public static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
13 | public static final String MYSQL_WHOAMI_QUERY = "SELECT USER()";
14 | public static final String MYSQL_DBNAME_QUERY = "SELECT DATABASE()";
15 |
16 | public static final String PGSQL = "PGSQL";
17 | public static final String PGSQL_VALIDATION_QUERY = "SELECT 1";
18 | public static final String PGSQL_WHOAMI_QUERY = "SELECT CURRENT_USER";
19 | public static final String PGSQL_DBNAME_QUERY = "SELECT CURRENT_DATABASE()";
20 | public static final String PGSQL_DRIVER = "org.postgresql.Driver";
21 |
22 | public static final String DB2 = "DB2";
23 | public static final String DB2_VALIDATION_QUERY = "SELECT 1 FROM SYSIBM.DUAL";
24 | public static final String DB2_WHOAMI_QUERY = " SELECT SYSTEM_USER FROM SYSIBM.DUAL";
25 | public static final String DB2_DBNAME_QUERY = "SELEC T DB_NAME FROM TABLE(SNAP_GET_DB('',-1))";
26 | public static final String DB2_DRIVER = "com.ibm.db2.jcc.DB2Driver";
27 |
28 | public static final String MSSQL = "MSSQL";
29 | public static final String MSSQL_VALIDATION_QUERY = "SELECT 1";
30 | public static final String MSSQL_WHOAMI_QUERY = "SELECT CURRENT_USER";
31 | public static final String MSSQL_DBNAME_QUERY = "SELECT db_name()";
32 | public static final String MSSQL_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
33 |
34 | public static final String SQLANY = "SQLANY" ;
35 | public static final String SQLANY_VALIDATION_QUERY = "SELECT 1";
36 | public static final String SQLANY_WHOAMI_QUERY = "SELECT CURRENT_USER";
37 | public static final String SQLANY_DBNAME_QUERY = "SELECT db_name()";
38 | public static final String SQLANY_DRIVER = "com.sybase.jdbc4.jdbc.SybDriver";
39 |
40 | public static final String SYBASE = "SYBASE";
41 | public static final String SYBASE_VALIDATION_QUERY = "SELECT 1";
42 | public static final String SYBASE_WHOAMI_QUERY = "SELECT suser_name()";
43 | public static final String SYBASE_DBNAME_QUERY = "SELECT db_name()";
44 | public static final String SYBASE_DRIVER = "net.sourceforge.jtds.jdbc.Driver";
45 |
46 | public static final String ALLBASE = "ALLBASE";
47 | public static final String ALLBASE_VALIDATION_QUERY = "SELECT 1 FROM MGR@GUSSNM.DUMMY";
48 | public static final String ALLBASE_WHOAMI_QUERY = "SELECT 'User' FROM MGR@GUSSNM.DUMMY";
49 | public static final String ALLBASE_DBNAME_QUERY = "SELECT 'DBName' FROM MGR@GUSSNM.DUMMY";
50 | public static final String ALLBASE_DRIVER = "com.mbf.jdbc.MBFDriver";
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/DBManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db;
19 |
20 | import java.util.ArrayList;
21 | import java.util.Collection;
22 | import java.util.HashSet;
23 | import java.util.Set;
24 |
25 | import com.smartmarmot.dbforbix.config.Database;
26 | import com.smartmarmot.dbforbix.db.adapter.ALLBASE;
27 | import com.smartmarmot.dbforbix.db.adapter.DBAdapter;
28 | import com.smartmarmot.dbforbix.db.adapter.DB2;
29 | import com.smartmarmot.dbforbix.db.adapter.DB_NOT_DEFINED;
30 | import com.smartmarmot.dbforbix.db.adapter.MSSQL;
31 | import com.smartmarmot.dbforbix.db.adapter.MySQL;
32 | import com.smartmarmot.dbforbix.db.adapter.Oracle;
33 | import com.smartmarmot.dbforbix.db.adapter.PGSQL;
34 | import com.smartmarmot.dbforbix.db.adapter.SQLANY;
35 | import com.smartmarmot.dbforbix.db.adapter.SYBASE;
36 |
37 | public class DBManager {
38 |
39 | private static DBManager instance;
40 |
41 | protected DBManager() {}
42 |
43 | private Set databases = new HashSet<>();
44 |
45 | public static DBManager getInstance() {
46 | if (instance == null)
47 | instance = new DBManager();
48 | return instance;
49 | }
50 |
51 | //DB2, ORACLE, MSSQL, MYSQL, PGSQL, ALLBASE, SYBASE, SQLANY;
52 | public void addDatabase(Database cfg) {
53 | if(databases.contains(cfg.getDBNameFC()))
54 | return;
55 | switch (cfg.getType()) {
56 | case DB2:
57 | databases.add(new DB2(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(), cfg.getMaxActive(),cfg.getMaxIdle()
58 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
59 | break;
60 | case ORACLE:
61 | databases.add(new Oracle(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(),cfg.getMaxActive(),cfg.getMaxIdle()
62 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
63 | break;
64 | case MSSQL:
65 | databases.add(new MSSQL(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(),cfg.getMaxActive(),cfg.getMaxIdle()
66 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
67 | break;
68 | case MYSQL:
69 | databases.add(new MySQL(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(),cfg.getMaxActive(),cfg.getMaxIdle()
70 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
71 | break;
72 | case PGSQL:
73 | databases.add(new PGSQL(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(),cfg.getMaxActive(),cfg.getMaxIdle()
74 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
75 | break;
76 | case ALLBASE:
77 | databases.add(new ALLBASE(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(),cfg.getMaxActive(),cfg.getMaxIdle()
78 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
79 | break;
80 | case SYBASE:
81 | databases.add(new SYBASE(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(),cfg.getMaxActive(),cfg.getMaxIdle()
82 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
83 | break;
84 | case SQLANY:
85 | databases.add(new SQLANY(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(),cfg.getMaxActive(),cfg.getMaxIdle()
86 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
87 | break;
88 | case DB_NOT_DEFINED:
89 | databases.add(new DB_NOT_DEFINED(cfg.getDBNameFC(), cfg.getURL(), cfg.getUser(), cfg.getPassword(),cfg.getMaxActive(),cfg.getMaxIdle()
90 | ,cfg.getMaxWaitMillis(),cfg.getQueryTimeout(),cfg.getConfigurationUIDs(),cfg.getPersistence()));
91 | break;
92 | }
93 | }
94 |
95 | /**
96 | * Get databases by configurationUID
97 | * @param configurationUID
98 | * @return array of DBs
99 | */
100 | public DBAdapter[] getDBsByConfigurationUID(String configurationUID) {
101 | ArrayList result = new ArrayList(databases.size());
102 | for (DBAdapter db : databases) {
103 | if (db.getConfigurationUIDs().contains(configurationUID))
104 | result.add(db);
105 | }
106 | return result.toArray(new DBAdapter[result.size()]);
107 | }
108 |
109 | public DBAdapter[] getDatabases() {
110 | return databases.toArray(new DBAdapter[databases.size()]);
111 | }
112 |
113 | public DBManager cleanAll() {
114 | Set configurationUIDs=new HashSet<>();
115 | for(DBAdapter db:getDatabases()){
116 | configurationUIDs.addAll(db.getConfigurationUIDs());
117 | }
118 | return clean(configurationUIDs);
119 | }
120 |
121 | public DBManager clean(Collection configurationUIDs) {
122 | if(!configurationUIDs.isEmpty()){
123 | for(DBAdapter db:getDatabases()){
124 | db.getConfigurationUIDs().removeAll(configurationUIDs);
125 | if(db.getConfigurationUIDs().isEmpty())
126 | db.abort();
127 | }
128 | java.util.function.Predicate dbPredicate=(DBAdapter db)-> db.getConfigurationUIDs().isEmpty();
129 | databases.removeIf(dbPredicate);
130 | if(databases.isEmpty())
131 | instance=null;
132 | }
133 | return getInstance();
134 | }
135 |
136 |
137 | public DBAdapter getDatabaseByName(String dbNameFC) {
138 | DBAdapter result=null;
139 | for(DBAdapter db:databases){
140 | if(db.getName().equals(dbNameFC)){
141 | result=db;
142 | break;
143 | }
144 | }
145 | return result;
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/DBType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db;
19 |
20 | /**
21 | * SQL database type
22 | *
23 | * @author Andrea Dalle Vacche
24 | */
25 | public enum DBType {
26 |
27 | DB2, ORACLE, MSSQL, MYSQL, PGSQL, ALLBASE, SYBASE, SQLANY, DB_NOT_DEFINED;
28 |
29 | /**
30 | * Parses a string and returns a matching database type object
31 | * @param data
32 | * @return
33 | */
34 |
35 | public static DBType fromString(String data) {
36 | if (DBConstants.DB2.equalsIgnoreCase(data))
37 | return DB2;
38 | if (DBConstants.ORACLE.equalsIgnoreCase(data))
39 | return ORACLE;
40 | if (DBConstants.MSSQL.equalsIgnoreCase(data))
41 | return MSSQL;
42 | if (DBConstants.MYSQL.equalsIgnoreCase(data))
43 | return MYSQL;
44 | if (DBConstants.PGSQL.equalsIgnoreCase(data))
45 | return PGSQL;
46 | if (DBConstants.ALLBASE.equalsIgnoreCase(data))
47 | return ALLBASE;
48 | if (DBConstants.SYBASE.equalsIgnoreCase(data))
49 | return SYBASE;
50 | if (DBConstants.SQLANY.equalsIgnoreCase(data))
51 | return SQLANY;
52 | return null;
53 | }
54 |
55 | /**
56 | * @return the known JDBC driver class name
57 | * DB2, ORACLE, MSSQL, MYSQL, PGSQL, ALLBASE, SYBASE, SQLANY;
58 | */
59 | public String getJDBCDriverClass() {
60 | switch (this) {
61 | case DB2:
62 | return DBConstants.DB2_DRIVER;
63 | case ORACLE:
64 | return DBConstants.ORACLE_DRIVER;
65 | case MSSQL:
66 | return DBConstants.MSSQL_DRIVER;
67 | case MYSQL:
68 | return DBConstants.MYSQL_DRIVER;
69 | case PGSQL:
70 | return DBConstants.PGSQL_DRIVER;
71 | case ALLBASE :
72 | return DBConstants.ALLBASE_DRIVER;
73 | case SYBASE :
74 | return DBConstants.SYBASE_DRIVER;
75 | case SQLANY :
76 | return DBConstants.SQLANY_DRIVER;
77 | default:
78 | return null;
79 | }
80 | }
81 |
82 | /**
83 | * @return A simple statement to check if the database respond to queries
84 | * DB2, ORACLE, MSSQL, MYSQL, PGSQL, ALLBASE, SYBASE, SQLANY;
85 | */
86 | public String getAliveSQL() {
87 | switch (this) {
88 | case DB2:
89 | return DBConstants.DB2_VALIDATION_QUERY;
90 | case ORACLE:
91 | return DBConstants.ORACLE_VALIDATION_QUERY;
92 | case MSSQL:
93 | return DBConstants.MSSQL_VALIDATION_QUERY;
94 | case MYSQL:
95 | return DBConstants.MYSQL_VALIDATION_QUERY;
96 | case PGSQL:
97 | return DBConstants.PGSQL_VALIDATION_QUERY;
98 | case ALLBASE :
99 | return DBConstants.ALLBASE_VALIDATION_QUERY;
100 | case SYBASE :
101 | return DBConstants.SYBASE_VALIDATION_QUERY;
102 | case SQLANY :
103 | return DBConstants.SQLANY_VALIDATION_QUERY;
104 | default:
105 | return null;
106 | }
107 | }
108 |
109 | /**
110 | * @return A query that returns the current user in the database
111 | * DB2, ORACLE, MSSQL, MYSQL, PGSQL, ALLBASE, SYBASE, SQLANY;
112 | */
113 | public String getWhoAmISQL() {
114 | switch (this) {
115 | case DB2:
116 | return DBConstants.DB2_WHOAMI_QUERY;
117 | case ORACLE:
118 | return DBConstants.ORACLE_WHOAMI_QUERY;
119 | case MSSQL:
120 | return DBConstants.MSSQL_WHOAMI_QUERY;
121 | case MYSQL:
122 | return DBConstants.MYSQL_WHOAMI_QUERY;
123 | case PGSQL:
124 | return DBConstants.PGSQL_WHOAMI_QUERY;
125 | case ALLBASE :
126 | return DBConstants.ALLBASE_WHOAMI_QUERY;
127 | case SYBASE :
128 | return DBConstants.SYBASE_WHOAMI_QUERY;
129 | case SQLANY :
130 | return DBConstants.SQLANY_WHOAMI_QUERY;
131 | default:
132 | return null;
133 | }
134 | }
135 |
136 | /**
137 | * @return A query which returns the current database name
138 | * DB2, ORACLE, MSSQL, MYSQL, PGSQL, ALLBASE, SYBASE, SQLANY;
139 | */
140 | public String getDbNameSQL() {
141 | switch (this) {
142 | case DB2:
143 | return DBConstants.DB2_DBNAME_QUERY;
144 | case ORACLE:
145 | return DBConstants.ORACLE_DBNAME_QUERY;
146 | case MSSQL:
147 | return DBConstants.MSSQL_DBNAME_QUERY;
148 | case MYSQL:
149 | return DBConstants.MYSQL_DBNAME_QUERY;
150 | case PGSQL:
151 | return DBConstants.PGSQL_DBNAME_QUERY;
152 | case ALLBASE :
153 | return DBConstants.ALLBASE_DBNAME_QUERY;
154 | case SYBASE :
155 | return DBConstants.SYBASE_DBNAME_QUERY;
156 | case SQLANY :
157 | return DBConstants.SQLANY_DBNAME_QUERY;
158 | default:
159 | return null;
160 | }
161 | }
162 |
163 | }
164 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/ALLBASE.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.db.adapter;
2 |
3 | import java.util.Set;
4 |
5 | import com.smartmarmot.dbforbix.db.DBType;
6 |
7 |
8 | public class ALLBASE extends AbstractDBAdapter {
9 |
10 | public ALLBASE(String name, String url, String user, String passwd, Integer maxactive, Integer maxidle,Integer maxwaitmillis,Integer queryTimeout, Set set, Boolean pers) {
11 | this.name = name;
12 | this.url = url;
13 | this.user = user;
14 | this.passwd = passwd;
15 | this.maxactive = maxactive.intValue();
16 | this.maxidle = maxidle.intValue();
17 | this.maxwaitmillis=maxwaitmillis.intValue();
18 | this.queryTimeout = queryTimeout.intValue();
19 | this.persistence = pers;
20 | this.configurationUIDs=set;
21 | }
22 |
23 | @Override
24 | public DBType getType() {
25 | return DBType.ALLBASE;
26 | }
27 |
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/AbstractDBAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db.adapter;
19 |
20 | import java.sql.Connection;
21 | import java.sql.SQLException;
22 | import java.util.HashSet;
23 | import java.util.Set;
24 |
25 | import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS;
26 | import org.apache.commons.dbcp2.datasources.SharedPoolDataSource;
27 | import org.apache.log4j.Logger;
28 |
29 | import com.smartmarmot.dbforbix.config.Config;
30 | import com.smartmarmot.dbforbix.db.DBType;
31 |
32 | abstract class AbstractDBAdapter implements DBAdapter {
33 |
34 | private static final Logger LOG = Logger.getLogger(AbstractDBAdapter.class);
35 |
36 | private SharedPoolDataSource datasrc;
37 |
38 | protected String name;
39 | protected String url;
40 | protected String user;
41 | protected String passwd;
42 | protected int maxactive;
43 | protected int maxidle;
44 | protected int maxwaitmillis;
45 | protected int queryTimeout;
46 |
47 |
48 | protected boolean persistence;
49 | protected Set configurationUIDs=new HashSet();
50 |
51 | public Set getConfigurationUIDs() {
52 | return configurationUIDs;
53 | }
54 |
55 | public void addConfigurationUID(String configurationUID) {
56 | configurationUIDs.add(configurationUID);
57 | }
58 |
59 |
60 | private void createConnection() throws SQLException, ClassNotFoundException{
61 | LOG.info("Creating new connection pool for database " + getName());
62 | Config cfg=Config.getInstance();
63 | DriverAdapterCPDS cpds = new DriverAdapterCPDS();
64 | cpds.setDriver(getType().getJDBCDriverClass());
65 | cpds.setUrl(getURL());
66 | cpds.setUser(getUser());
67 | cpds.setPassword(getPassword());
68 | datasrc = new SharedPoolDataSource();
69 | datasrc.setConnectionPoolDataSource(cpds);
70 | datasrc.setLoginTimeout(cfg.getLoginTimeout());
71 | datasrc.setMaxTotal(getMaxActive());
72 | datasrc.setDefaultMaxIdle(getMaxIdle());
73 | datasrc.setDefaultMaxWaitMillis(getMaxWaitMillis());
74 | datasrc.setValidationQuery(getType().getAliveSQL());
75 | datasrc.setDefaultTestOnBorrow(true);
76 | /**
77 | * wait while connection is initialized
78 | */
79 | try {
80 | Thread.sleep(1000);
81 | } catch (InterruptedException e) {
82 | e.printStackTrace();
83 | }
84 | }
85 |
86 | @Override
87 | public Connection getConnection() throws SQLException, ClassNotFoundException, DBNotDefinedException {
88 | if(DBType.DB_NOT_DEFINED == this.getType())
89 | throw new DBNotDefinedException("Database "+getName()+" hasn't been defined in DBforBix local file config yet!");
90 | if (datasrc == null ) createConnection();
91 | return datasrc.getConnection();
92 | }
93 |
94 | @Override
95 | public void reconnect(){
96 | LOG.warn("Trying to reconnect...");
97 | abort();
98 | try {
99 | createConnection();
100 | LOG.warn("Reconnected.");
101 | } catch (ClassNotFoundException | SQLException e) {
102 | LOG.warn("Reconnection has failed.");
103 | e.printStackTrace();
104 | try {
105 | LOG.warn("Sleeping 60 seconds...");
106 | Thread.sleep(60000);
107 | } catch (InterruptedException e1) {
108 | e1.printStackTrace();
109 | }
110 | }
111 | }
112 |
113 | @Override
114 | public void abort(){
115 | try {
116 | if(null!=datasrc) datasrc.close();
117 | } catch (Exception e) {
118 | e.printStackTrace();
119 | }
120 | datasrc=null;
121 | }
122 |
123 | @Override
124 | public String getName() {
125 | return name;
126 | }
127 |
128 | @Override
129 | public String getURL() {
130 | return url;
131 | }
132 |
133 | @Override
134 | public String getUser() {
135 | return user;
136 | }
137 |
138 | @Override
139 | public String getPassword() {
140 | return passwd;
141 | }
142 |
143 | @Override
144 | public Integer getMaxActive() {
145 | return maxactive;
146 | }
147 | public Integer getMaxIdle() {
148 | return maxidle;
149 | }
150 | @Override
151 | public Integer getMaxWaitMillis() {
152 | return maxwaitmillis;
153 | }
154 | public boolean getPersistence() {
155 | return persistence;
156 | }
157 |
158 | @Override
159 | public Integer getQueryTimeout() {
160 | return queryTimeout;
161 | }
162 |
163 | @Override
164 | public String[] getDiscoveryItems() {
165 | return new String[0];
166 | }
167 |
168 | @Override
169 | public Object getDiscovery(String key) {
170 | throw new UnsupportedOperationException();
171 | }
172 |
173 | @Override
174 | public boolean hasServerItems() {
175 | return false;
176 | }
177 |
178 | @Override
179 | public boolean hasUserItems() {
180 | return false;
181 | }
182 |
183 | public boolean hasPersistence() {
184 | return this.persistence;
185 | }
186 |
187 | @Override
188 | public boolean hasDatabaseItems() {
189 | return false;
190 | }
191 |
192 | @Override
193 | public boolean hasSchemaItems() {
194 | return false;
195 | }
196 |
197 | @Override
198 | public boolean hasTablespaceItems() {
199 | return false;
200 | }
201 | }
202 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/DB2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db.adapter;
19 |
20 | import java.util.Set;
21 |
22 | import com.smartmarmot.dbforbix.db.DBType;
23 |
24 |
25 | public class DB2 extends AbstractDBAdapter {
26 |
27 | public DB2(String name, String url, String user, String passwd, Integer maxactive, Integer maxidle,Integer maxwaitmillis, Integer queryTimeout, Set set, Boolean pers) {
28 | this.name = name;
29 | this.url = url;
30 | this.user = user;
31 | this.passwd = passwd;
32 | this.maxactive = maxactive.intValue();
33 | this.maxidle = maxidle.intValue();
34 | this.maxwaitmillis = maxwaitmillis.intValue();
35 | this.queryTimeout = queryTimeout.intValue();
36 | this.persistence = pers;
37 | this.configurationUIDs=set;
38 |
39 | }
40 |
41 | @Override
42 | public DBType getType() {
43 | return DBType.DB2;
44 | }
45 |
46 |
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/DBAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db.adapter;
19 |
20 | import java.sql.Connection;
21 | import java.sql.SQLException;
22 | import java.util.Set;
23 |
24 | import com.smartmarmot.dbforbix.db.DBType;
25 |
26 | public interface DBAdapter {
27 |
28 | public DBType getType();
29 | public String getName();
30 | public String getURL();
31 | public String getUser();
32 | public String getPassword();
33 | public Integer getMaxActive();
34 | public Integer getMaxIdle();
35 | public Integer getMaxWaitMillis();
36 | public Integer getQueryTimeout();
37 | //public String getItemFile();
38 | public Set getConfigurationUIDs();
39 |
40 | public String[] getDiscoveryItems();
41 | public Object getDiscovery(String key);
42 |
43 | public boolean hasServerItems();
44 | public boolean hasUserItems();
45 | public boolean hasDatabaseItems();
46 | public boolean hasSchemaItems();
47 | public boolean hasTablespaceItems();
48 | public boolean getPersistence();
49 |
50 |
51 | /**
52 | * Exception if database that came from Zabbix Web has not been defined in local file config yet.
53 | */
54 | public class DBNotDefinedException extends RuntimeException
55 | {
56 | private static final long serialVersionUID = -106570768147815384L;
57 | //Parameterless Constructor
58 | public DBNotDefinedException() {super("Database hasn't been defined in DBforBix local file config yet!");}
59 | //Constructors that accept parameters
60 | public DBNotDefinedException(String msg) { super(msg); }
61 | public DBNotDefinedException(Throwable cause) { super(cause); }
62 | public DBNotDefinedException(String msg, Throwable cause) { super(msg, cause); }
63 | }
64 | public Connection getConnection() throws SQLException, ClassNotFoundException, DBNotDefinedException;
65 | public void abort();
66 | public void reconnect();
67 |
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/DB_NOT_DEFINED.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.db.adapter;
2 |
3 | import java.util.Set;
4 |
5 | import com.smartmarmot.dbforbix.db.DBType;
6 |
7 | public class DB_NOT_DEFINED extends AbstractDBAdapter {
8 |
9 | public DB_NOT_DEFINED(String name, String url, String user, String passwd,Integer maxactive, Integer maxidle,Integer maxwaitmillis, Integer queryTimeout, Set set, Boolean pers) {
10 | this.name = name;
11 | this.url = url;
12 | this.user = user;
13 | this.passwd = passwd;
14 | this.maxactive = maxactive.intValue();
15 | this.maxidle = maxidle.intValue();
16 | this.maxwaitmillis=maxwaitmillis.intValue();
17 | this.queryTimeout = queryTimeout.intValue();
18 | this.persistence = pers;
19 | this.configurationUIDs=set;
20 | }
21 |
22 |
23 |
24 | @Override
25 | public DBType getType() {
26 | return DBType.DB_NOT_DEFINED;
27 | }
28 |
29 |
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/MSSQL.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db.adapter;
19 |
20 | import java.util.Set;
21 |
22 | import com.smartmarmot.dbforbix.db.DBType;
23 |
24 | public class MSSQL extends AbstractDBAdapter {
25 | public MSSQL(String name, String url, String user, String passwd, Integer maxactive, Integer maxidle, Integer maxwaitmillis, Integer queryTimeout, Set set, Boolean pers) {
26 | this.name = name;
27 | this.url = url;
28 | this.user = user;
29 | this.passwd = passwd;
30 | this.maxactive = maxactive.intValue();
31 | this.maxidle = maxidle.intValue();
32 | this.maxwaitmillis=maxwaitmillis.intValue();
33 | this.queryTimeout = queryTimeout.intValue();
34 | this.configurationUIDs = set;
35 | this.persistence = pers;
36 |
37 | }
38 |
39 | @Override
40 | public DBType getType() {
41 | return DBType.MSSQL;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/MySQL.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db.adapter;
19 |
20 | import java.util.Set;
21 |
22 | import com.smartmarmot.dbforbix.db.DBType;
23 |
24 | public class MySQL extends AbstractDBAdapter {
25 |
26 | public MySQL(String name, String url, String user, String passwd, Integer maxactive, Integer maxidle, Integer maxwaitmillis, Integer queryTimeout, Set set,Boolean pers) {
27 | this.name = name;
28 | this.url = url;
29 | this.user = user;
30 | this.passwd = passwd;
31 | this.maxactive = maxactive.intValue();
32 | this.maxidle = maxidle.intValue();
33 | this.maxwaitmillis=maxwaitmillis.intValue();
34 | this.queryTimeout = queryTimeout.intValue();
35 | this.configurationUIDs = set;
36 | this.persistence = pers;
37 |
38 | }
39 |
40 | @Override
41 | public DBType getType() {
42 | return DBType.MYSQL;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/Oracle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db.adapter;
19 |
20 | import java.util.Set;
21 |
22 | import com.smartmarmot.dbforbix.db.DBType;
23 |
24 |
25 | public class Oracle extends AbstractDBAdapter {
26 |
27 | //Error codes
28 | //ORA-12537 - connection closed
29 | public static final int ConnectionClosed=12537;
30 |
31 | public Oracle(String name, String url, String user, String passwd, Integer maxactive,Integer maxidle,Integer maxwaitmillis, Integer queryTimeout, Set set, Boolean pers) {
32 | this.name = name;
33 | this.url = url;
34 | this.user = user;
35 | this.passwd = passwd;
36 | this.maxactive = maxactive;
37 | this.maxwaitmillis = maxwaitmillis;
38 | this.queryTimeout = queryTimeout.intValue();
39 | this.maxidle = maxidle;
40 | this.configurationUIDs = set;
41 | this.persistence = pers;
42 | }
43 |
44 |
45 |
46 | @Override
47 | public DBType getType() {
48 | return DBType.ORACLE;
49 | }
50 |
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/PGSQL.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.db.adapter;
19 |
20 | import java.util.Set;
21 |
22 | import com.smartmarmot.dbforbix.db.DBType;
23 |
24 |
25 | public class PGSQL extends AbstractDBAdapter {
26 |
27 | public PGSQL(String name, String url, String user, String passwd, Integer maxactive, Integer maxidle, Integer maxwaitmillis, Integer queryTimeout, Set set,Boolean pers) {
28 | this.name = name;
29 | this.url = url;
30 | this.user = user;
31 | this.passwd = passwd;
32 | this.maxactive = maxactive.intValue();
33 | this.maxidle = maxidle.intValue();
34 | this.maxwaitmillis=maxwaitmillis.intValue();
35 | this.queryTimeout = queryTimeout.intValue();
36 | this.configurationUIDs = set;
37 | this.persistence = pers;
38 | }
39 |
40 | @Override
41 | public DBType getType() {
42 | return DBType.PGSQL;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/SQLANY.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.db.adapter;
2 |
3 | import java.util.Set;
4 |
5 | import com.smartmarmot.dbforbix.db.DBType;
6 |
7 |
8 | public class SQLANY extends AbstractDBAdapter {
9 |
10 | public SQLANY(String name, String url, String user, String passwd,Integer maxactive, Integer maxidle,Integer maxwaitmillis, Integer queryTimeout, Set set, Boolean pers) {
11 | this.name = name;
12 | this.url = url;
13 | this.user = user;
14 | this.passwd = passwd;
15 | this.maxactive = maxactive.intValue();
16 | this.maxidle = maxidle.intValue();
17 | this.maxwaitmillis=maxwaitmillis.intValue();
18 | this.queryTimeout = queryTimeout.intValue();
19 | this.persistence = pers;
20 | this.configurationUIDs=set;
21 | }
22 |
23 | @Override
24 | public DBType getType() {
25 | return DBType.SQLANY;
26 | }
27 |
28 |
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/db/adapter/SYBASE.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.db.adapter;
2 |
3 | import java.util.Set;
4 |
5 | import com.smartmarmot.dbforbix.db.DBType;
6 |
7 |
8 | public class SYBASE extends AbstractDBAdapter {
9 |
10 | public SYBASE(String name, String url, String user, String passwd,Integer maxactive, Integer maxidle,Integer maxwaitmillis, Integer queryTimeout, Set set,Boolean pers) {
11 | this.name = name;
12 | this.url = url;
13 | this.user = user;
14 | this.passwd = passwd;
15 | this.maxactive = maxactive.intValue();
16 | this.maxidle = maxidle.intValue();
17 | this.maxwaitmillis=maxwaitmillis.intValue();
18 | this.queryTimeout = queryTimeout.intValue();
19 | this.configurationUIDs=set;
20 | this.persistence = pers;
21 | }
22 |
23 | @Override
24 | public DBType getType() {
25 | return DBType.SYBASE;
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/scheduler/Scheduler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.scheduler;
19 |
20 | import java.sql.Connection;
21 | import java.sql.SQLException;
22 | import java.sql.SQLTimeoutException;
23 | import java.util.HashSet;
24 | import java.util.Set;
25 | import java.util.TimerTask;
26 | import org.apache.log4j.Logger;
27 |
28 | import com.smartmarmot.dbforbix.DBforBix;
29 | import com.smartmarmot.dbforbix.config.element.IConfigurationElement;
30 | import com.smartmarmot.dbforbix.db.DBManager;
31 | import com.smartmarmot.dbforbix.db.DBType;
32 | import com.smartmarmot.dbforbix.db.adapter.DBAdapter;
33 | import com.smartmarmot.dbforbix.db.adapter.DBAdapter.DBNotDefinedException;
34 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
35 | import com.smartmarmot.dbforbix.zabbix.ZabbixSender;
36 |
37 | /**
38 | * The item fetching class
39 | *
40 | * @author Andrea Dalle Vacche
41 | */
42 | public class Scheduler extends TimerTask {
43 |
44 | private static final Logger LOG = Logger.getLogger(Scheduler.class);
45 | private boolean working = false;
46 | private int pause;
47 |
48 | private Set configurationElements = new HashSet<>();
49 |
50 | /**
51 | * Creates a new TimeTask for item fetching
52 | *
53 | * @param taskGroup
54 | * the schedule group of this worker
55 | * @param databaseCfg
56 | * the database config to use
57 | */
58 | public Scheduler(int pause) {
59 | this.pause = pause;
60 | }
61 |
62 | public void addConfigurationElement(IConfigurationElement configurationElement) {
63 | configurationElements.add(configurationElement);
64 | }
65 |
66 | public int getPause() {
67 | return pause;
68 | }
69 |
70 | @Override
71 | public void run() {
72 | if (working)
73 | return;
74 | working = true;
75 | DBManager dbManager = DBManager.getInstance();
76 | ZabbixSender zabbixSender = DBforBix.getZabbixSender();
77 | try {
78 | LOG.debug("Scheduler.run() " + getPause());
79 | // :
80 | for (IConfigurationElement configurationElement : configurationElements) {
81 | // -> DBs monitored
82 | DBAdapter[] targetDB = dbManager.getDBsByConfigurationUID(configurationElement.getConfigurationItem().getConfigurationUID());
83 | if (targetDB != null && targetDB.length > 0) {
84 | for (DBAdapter db : targetDB) {
85 | try(Connection dbConnection = db.getConnection()) {
86 | try {
87 | ZabbixItem[] result = configurationElement.getZabbixItemsData(dbConnection, db.getQueryTimeout());
88 | for (ZabbixItem i : result) zabbixSender.addItem(i);
89 | }
90 | catch (NullPointerException e){
91 | LOG.warn("No data has been returned for item "+ configurationElement.getElementID());
92 | }
93 | catch (SQLTimeoutException sqlex) {
94 | LOG.warn("Timeout after "+db.getQueryTimeout()+"s for item: " + configurationElement.getElementID(), sqlex);
95 | }
96 | catch (SQLException sqlex) {
97 | LOG.warn("could not fetch value of [" + configurationElement.getElementID() +"]\nError code: "+
98 | sqlex.getErrorCode()+"\nError message: "+sqlex.getLocalizedMessage()+"\n",
99 | sqlex);
100 | //propagate error to Zabbix Web interface
101 | zabbixSender.addItem(
102 | new ZabbixItem(
103 | configurationElement.getElementID(),
104 | "Could not fetch value of [" + configurationElement.getElementID() +"] for db "+ db.getName()+":\n"+sqlex.getLocalizedMessage(),
105 | ZabbixItem.ZBX_STATE_NOTSUPPORTED,
106 | new Long(System.currentTimeMillis() / 1000L),
107 | configurationElement
108 | )
109 | );
110 | //handle ORACLE closed connection exception
111 | if(DBType.ORACLE==db.getType()
112 | && sqlex.getLocalizedMessage().toLowerCase().contains("closed connection"))
113 | db.reconnect();
114 | }
115 | }
116 | catch(SQLException sqlex){
117 | LOG.error("Could not get connection to db: " + db.getName(), sqlex);
118 | zabbixSender.addItem(
119 | new ZabbixItem(
120 | configurationElement.getElementID(),
121 | "Could not connect to DB " + db.getName()+":\n"+sqlex.getLocalizedMessage(),
122 | ZabbixItem.ZBX_STATE_NOTSUPPORTED,
123 | new Long(System.currentTimeMillis() / 1000L),
124 | configurationElement
125 | )
126 | );
127 | }
128 | catch (DBNotDefinedException nodbex){
129 | LOG.error(nodbex.getLocalizedMessage());
130 | zabbixSender.addItem(
131 | new ZabbixItem(
132 | configurationElement.getElementID(),
133 | nodbex.getLocalizedMessage(),
134 | ZabbixItem.ZBX_STATE_NOTSUPPORTED,
135 | new Long(System.currentTimeMillis() / 1000L),
136 | configurationElement
137 | )
138 | );
139 | }
140 | }
141 | }
142 | }
143 | }
144 | catch(Exception ex){
145 | LOG.error("Scheduler exception: " + ex.getLocalizedMessage(),ex);
146 | }
147 | catch (Throwable th) {
148 | LOG.error("Scheduler - Error "+th.getLocalizedMessage());
149 | th.printStackTrace();
150 | }
151 | working = false;
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/PersistentDBSender.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.zabbix;
19 |
20 | import java.io.InputStream;
21 | import java.io.OutputStream;
22 | import java.io.OutputStreamWriter;
23 | import java.net.InetSocketAddress;
24 | import java.net.Socket;
25 |
26 | import org.apache.log4j.Logger;
27 |
28 | import com.smartmarmot.common.PersistentDB;
29 | import com.smartmarmot.dbforbix.config.ZabbixServer;
30 | import com.smartmarmot.dbforbix.zabbix.protocol.Sender18;
31 | import com.smartmarmot.dbforbix.zabbix.protocol.ISenderProtocol;
32 |
33 | /**
34 | * Sender query handler
35 | *
36 | * @author Andrea Dalle Vacche
37 | */
38 | public class PersistentDBSender extends Thread {
39 |
40 | public enum PROTOCOL {
41 | V14, V18
42 | }
43 |
44 | private static final Logger LOG = Logger.getLogger(PersistentDBSender.class);
45 | private boolean terminate = false;
46 | private ZabbixServer[] configuredServers = new ZabbixServer[0];
47 | private ISenderProtocol protocol;
48 |
49 |
50 | public PersistentDBSender(PROTOCOL protVer) {
51 | super("PersistentDBSender");
52 | switch (protVer) {
53 | default:
54 | protocol = new Sender18();
55 | break;
56 | }
57 | setDaemon(true);
58 | }
59 |
60 | @Override
61 | public void run() {
62 | LOG.debug("PersistentDBSender - starting sender thread");
63 | while (!terminate) {
64 | try {
65 | if (PersistentDB.getInstance().size() == 0L) {
66 | Thread.sleep(60000);
67 | }
68 | else {
69 | ZabbixServer[] servers;
70 | synchronized (configuredServers) {
71 | servers = configuredServers;
72 | }
73 |
74 | LOG.info("PersistentDBSender - retrieving the first element to send");
75 | while (PersistentDB.getInstance().size() != 0L ){
76 | LOG.info("PersistentDBSender - found "+PersistentDB.getInstance().size()+" persistent items to send");
77 | ZabbixItem zx = (ZabbixItem) PersistentDB.getInstance().pop();
78 | for (ZabbixServer serverConfig : servers) {
79 | try {
80 | Socket zabbix = null;
81 | OutputStreamWriter out = null;
82 | InputStream in = null;
83 | byte[] response = new byte[1024];
84 |
85 | zabbix = new Socket();
86 | zabbix.setSoTimeout(5000);
87 | zabbix.connect(new InetSocketAddress(serverConfig.getZServerHost(), serverConfig.getZServerPort()));
88 | OutputStream os = zabbix.getOutputStream();
89 | LOG.debug("PersistentDBSender - Sending to " +zx.getHost() + " Item=" + zx.getKey() + " Value=" + zx.getValue());
90 | String data = protocol.encodeItem(zx);
91 | out = new OutputStreamWriter(os);
92 | out.write(data);
93 | out.flush();
94 |
95 | in = zabbix.getInputStream();
96 | final int read = in.read(response);
97 | if (!protocol.isResponeOK(read, response))
98 | LOG.warn("PersistentDBSender - Received unexpected response '" + new String(response).trim() + "' for key '" + zx.getKey()
99 | + "'");
100 | in.close();
101 | out.close();
102 | zabbix.close();
103 | }
104 | catch (Exception ex) {
105 | LOG.error("PersistentDBSender - Error contacting Zabbix server " + configuredServers[0].getZServerHost() +" port "+ configuredServers[0].getZServerPort()+ " - " + ex.getMessage());
106 | LOG.debug("PersistentDBSender - Current PersistentDB size ="+PersistentDB.getInstance().size());
107 | LOG.info("PersistentDBSender - Adding the item Adding the item="+zx.getHost()+" key="+zx.getKey()+" value="+zx.getValue()+" clock="+zx.getClock()+ " back to the persisent stack");
108 | PersistentDB.getInstance().push(zx);
109 | LOG.info("PersistentDBSender - going to sleep for 1 minute");
110 | Thread.sleep(60000);
111 | }
112 |
113 | }
114 | }
115 | }
116 | }
117 | catch (Exception e) {
118 | LOG.debug("PersistentDBSender - issue "+e);
119 | }
120 | }
121 | }
122 |
123 | synchronized public void updateServerList(ZabbixServer[] newServers) {
124 | synchronized (configuredServers) {
125 | configuredServers = newServers;
126 | }
127 | }
128 |
129 | public void terminate() {
130 | terminate = true;
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/PersistentStackSender.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.zabbix;
19 |
20 | import java.io.InputStream;
21 | import java.io.OutputStream;
22 | import java.io.OutputStreamWriter;
23 | import java.net.InetSocketAddress;
24 | import java.net.Socket;
25 |
26 | import org.apache.log4j.Logger;
27 |
28 | import com.smartmarmot.common.StackSingletonPersistent;
29 | import com.smartmarmot.dbforbix.config.ZabbixServer;
30 | import com.smartmarmot.dbforbix.zabbix.protocol.Sender18;
31 | import com.smartmarmot.dbforbix.zabbix.protocol.ISenderProtocol;
32 |
33 | /**
34 | * Sender query handler
35 | *
36 | * @author Andrea Dalle Vacche
37 | */
38 | public class PersistentStackSender extends Thread {
39 |
40 | public enum PROTOCOL {
41 | V14, V18
42 | }
43 |
44 | private static final Logger LOG = Logger.getLogger(PersistentStackSender.class);
45 | private boolean terminate = false;
46 | private ZabbixServer[] configuredServers = new ZabbixServer[0];
47 | private ISenderProtocol protocol;
48 |
49 |
50 | public PersistentStackSender(PROTOCOL protVer) {
51 | super("PersistentStackSender");
52 | switch (protVer) {
53 | default:
54 | protocol = new Sender18();
55 | break;
56 | }
57 | setDaemon(true);
58 | }
59 |
60 | @Override
61 | public void run() {
62 | LOG.debug("PersistentStackSender - starting sender thread");
63 | while (!terminate) {
64 | try {
65 | if (StackSingletonPersistent.getInstance().peek() == null) {
66 | Thread.sleep(10000);
67 | }
68 | else {
69 | ZabbixServer[] servers;
70 | synchronized (configuredServers) {
71 | servers = configuredServers;
72 | }
73 |
74 | LOG.info("PersitenceStackSender - retrieving the first element to send");
75 | while (StackSingletonPersistent.getInstance().size() != 0 ){
76 | LOG.debug("PersistentStackSender - found "+StackSingletonPersistent.getInstance().size()+" persistent items to send");
77 | ZabbixItem zx = (ZabbixItem) StackSingletonPersistent.getInstance().pop();
78 | for (ZabbixServer serverConfig : servers) {
79 | try {
80 | Socket zabbix = null;
81 | OutputStreamWriter out = null;
82 | InputStream in = null;
83 | byte[] response = new byte[1024];
84 |
85 | zabbix = new Socket();
86 | zabbix.setSoTimeout(5000);
87 | zabbix.connect(new InetSocketAddress(serverConfig.getZServerHost(), serverConfig.getZServerPort()));
88 | OutputStream os = zabbix.getOutputStream();
89 | LOG.debug("PersistentStackSender - Sending to " +zx.getHost() + " Item=" + zx.getKey() + " Value=" + zx.getValue());
90 | String data = protocol.encodeItem(zx);
91 | out = new OutputStreamWriter(os);
92 | out.write(data);
93 | out.flush();
94 |
95 | in = zabbix.getInputStream();
96 | final int read = in.read(response);
97 | if (!protocol.isResponeOK(read, response))
98 | LOG.warn("PersistentStackSender - Received unexpected response '" + new String(response).trim() + "' for key '" + zx.getKey()
99 | + "'");
100 | in.close();
101 | out.close();
102 | zabbix.close();
103 | }
104 | catch (Exception ex) {
105 | LOG.error("PersistentStackSender - Error contacting Zabbix server " + configuredServers[0].getZServerHost() +" port "+ configuredServers[0].getZServerPort()+ " - " + ex.getMessage());
106 | LOG.debug("PersistentStackSender - Current PeristentStack size ="+StackSingletonPersistent.getInstance().size());
107 | LOG.info("PersistentStackSender - Adding the item Adding the item="+zx.getHost()+" key="+zx.getKey()+" value="+zx.getValue()+" clock="+zx.getClock()+ " back to the persisent stack");
108 | StackSingletonPersistent.getInstance().push(zx);
109 | LOG.info("PersistentStackSender - going to sleep for 1 minute");
110 | Thread.sleep(60000);
111 | }
112 |
113 | }
114 | }
115 | }
116 | }
117 | catch (Exception e) {
118 | LOG.debug("PeristentStack issue "+e);
119 | }
120 | }
121 | }
122 |
123 | synchronized public void updateServerList(ZabbixServer[] newServers) {
124 | synchronized (configuredServers) {
125 | configuredServers = newServers;
126 | }
127 | }
128 |
129 | public void terminate() {
130 | terminate = true;
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/ZabbixItem.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.zabbix;
19 |
20 | import java.io.Serializable;
21 |
22 | import com.smartmarmot.dbforbix.config.element.IConfigurationElement;
23 |
24 | public final class ZabbixItem implements Serializable {
25 |
26 | public static int ZBX_STATE_NORMAL=0;
27 | public static int ZBX_STATE_NOTSUPPORTED=1;
28 |
29 | private static final long serialVersionUID = 1374520722821228793L;
30 |
31 | private String key;
32 | private String value;
33 | private String host;
34 | private int state;
35 | private Long clock;
36 | private String lastlogsize;
37 | private IConfigurationElement configurationElement;
38 |
39 |
40 | //main case
41 | public ZabbixItem(String key, String value, int state, Long clock, IConfigurationElement configurationElement) {
42 | if (key == null || "".equals(key.trim()))
43 | throw new IllegalArgumentException("empty key");
44 | if (value == null)
45 | throw new IllegalArgumentException("null value for key '" + key + "'");
46 | if (configurationElement == null)
47 | throw new IllegalArgumentException("null configuration element for '" + host + "." + key + "'");
48 |
49 | this.key = key;
50 | this.value = value;
51 | this.clock = clock;
52 | this.host=configurationElement.getConfigurationItem().getHost();
53 | this.setConfigurationElement(configurationElement);
54 | this.setState(state);
55 | }
56 |
57 |
58 | //For persistence usage
59 | public ZabbixItem(String host, String key, String value, Long clock) {
60 | if (key == null || "".equals(key.trim()))
61 | throw new IllegalArgumentException("empty key");
62 | if (value == null)
63 | throw new IllegalArgumentException("null value for key '" + key + "'");
64 | if (configurationElement == null)
65 | throw new IllegalArgumentException("null configuration element for '" + host + "." + key + "'");
66 |
67 | this.key = key;
68 | this.value = value;
69 | this.clock = clock;
70 | this.host=host;
71 | this.configurationElement=null;
72 | }
73 |
74 |
75 |
76 |
77 | /**
78 | * @return The current hostname for this item.
79 | */
80 | public String getHost() {
81 | return host;
82 | }
83 |
84 | /**
85 | * @return The monitoring server's key for this item.
86 | */
87 | public String getKey() {
88 | return key;
89 | }
90 |
91 | /**
92 | * @return The current value for this item.
93 | */
94 | public String getValue() {
95 | return value;
96 | }
97 |
98 | public Long getClock() {
99 | return clock;
100 | }
101 |
102 |
103 | public String getLastlogsize() {
104 | return lastlogsize;
105 | }
106 |
107 | @Override
108 | public String toString() {
109 | return getHost() + " " + getKey() + ": " + getValue();
110 | }
111 |
112 | public IConfigurationElement getConfigurationElement() {
113 | return configurationElement;
114 | }
115 |
116 | public void setConfigurationElement(IConfigurationElement configurationElement) {
117 | this.configurationElement = configurationElement;
118 | }
119 |
120 |
121 | public int getState() {
122 | return state;
123 | }
124 |
125 |
126 | public void setState(int state) {
127 | this.state = state;
128 | }
129 |
130 |
131 | public void setValue(String value) {
132 | this.value = value;
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/ZabbixSender.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.zabbix;
19 |
20 | import java.util.Collection;
21 | import java.util.HashMap;
22 | import java.util.HashSet;
23 | import java.util.Map;
24 | import java.util.Queue;
25 | import java.util.Map.Entry;
26 | import java.util.concurrent.LinkedBlockingQueue;
27 |
28 | import org.apache.log4j.Logger;
29 |
30 | import com.smartmarmot.common.PersistentDB;
31 | import com.smartmarmot.common.StackSingletonPersistent;
32 | import com.smartmarmot.dbforbix.config.Config;
33 | import com.smartmarmot.dbforbix.config.ZabbixServer;
34 | import com.smartmarmot.dbforbix.zabbix.protocol.Sender32;
35 | import com.smartmarmot.dbforbix.zabbix.protocol.ISenderProtocol;
36 |
37 | /**
38 | * Sender query handler
39 | *
40 | * @author Andrea Dalle Vacche
41 | */
42 | public class ZabbixSender extends Thread {
43 |
44 | public enum PROTOCOL {
45 | V14, V18, V32
46 | }
47 |
48 | private static final Logger LOG = Logger.getLogger(ZabbixSender.class);
49 |
50 | private Queue items = new LinkedBlockingQueue(1000);
51 | private boolean terminate = false;
52 | private ZabbixServer[] configuredServers = new ZabbixServer[0];
53 | private ISenderProtocol protocol;
54 |
55 | public ZabbixSender(PROTOCOL protVer) {
56 | super("ZabbixSender");
57 | switch (protVer) {
58 | default:
59 | protocol = new Sender32();
60 | break;
61 | }
62 | setDaemon(true);
63 | }
64 |
65 | @Override
66 | public void run() {
67 | LOG.debug("ZabbixSender - starting sender thread");
68 | while (!terminate) {
69 | if (items.peek() == null) {
70 | try {
71 | Thread.sleep(100);
72 | }
73 | catch (InterruptedException e) {}
74 | }
75 | else {
76 | //TODO maxItems should be taken from configuration file
77 | //take bulk of items to send
78 | int maxItems=100;
79 | //ZabbixItem[] itemsReady=new ZabbixItem[maxItems];
80 | /**
81 | * Put item to corresponding Zabbix Server
82 | */
83 | Map> mZServer2ZItems = new HashMap<>();
84 | for(int i=0;(i());
95 | mZServer2ZItems.get(zs).add(nextItem);
96 | }
97 |
98 | for(Entry> m:mZServer2ZItems.entrySet()){
99 | LOG.debug("ZabbixSender: Sending to " + m.getKey() + " Items[" + m.getValue().size() + "]=" + m.getValue());
100 | }
101 |
102 | Config config = Config.getInstance();
103 |
104 | for (Entry> entry : mZServer2ZItems.entrySet()) {
105 | ZabbixServer zs=entry.getKey();
106 | Collection zItems=entry.getValue();
107 | // Collection zDiscoveries= new HashSet();
108 | // Collection zHistories = new HashSet();
109 | //
110 | // // separate Discovery and History data: they should be run in different requests with different types
111 | // for(ZabbixItem zItem:zItems){
112 | // if(zItem.getConfItem() instanceof Discovery){
113 | // zDiscoveries.add(zItem);
114 | // }else{
115 | // zHistories.add(zItem);
116 | // }
117 | // }
118 | boolean persistent = false;
119 | for (int i = 0; i < 3; ++i) {
120 | String resp=new String();
121 | try {
122 | String data = protocol.encodeItems(zItems.toArray(new ZabbixItem[0]));
123 | LOG.debug("ZabbixSender[data]: "+data);
124 | resp=config.requestZabbix(zs.getZServerHost(),zs.getZServerPort(),data);
125 | LOG.debug("ZabbixSender[resp]: "+resp);
126 | break;
127 | }
128 | catch (Exception ex) {
129 | LOG.error("ZabbixSender: Error contacting Zabbix server " + zs.getZServerHost() + " - " + ex.getMessage());
130 | if (persistent){
131 | LOG.debug("ZabbixSender: Current PeristentStack size ="+StackSingletonPersistent.getInstance().size());
132 | LOG.info("ZabbixSender - Adding to the persisent stack items: "+zItems);
133 | PersistentDB.getInstance().add(zItems);
134 | LOG.debug("ZabbixSender - Current PersistentDB size ="+PersistentDB.getInstance().size());
135 | }
136 | }
137 | finally{
138 | persistent = false;
139 | }
140 | }
141 | }
142 | }
143 | }
144 | }
145 |
146 | public void addItem(ZabbixItem item) {
147 | if (items.size() < 1000)
148 | items.offer(item);
149 | }
150 |
151 | synchronized public void updateServerList(ZabbixServer[] newServers) {
152 | synchronized (configuredServers) {
153 | configuredServers = newServers;
154 | }
155 | }
156 |
157 | public void terminate() {
158 | terminate = true;
159 | }
160 | }
161 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/protocol/ISenderProtocol.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.zabbix.protocol;
19 |
20 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
21 |
22 |
23 | public interface ISenderProtocol {
24 | public boolean isMultiValueSupported();
25 |
26 | public String encodeItem(ZabbixItem item);
27 |
28 | public String encodeItems(ZabbixItem[] items);
29 |
30 | public boolean isResponeOK(int readed, byte[] response);
31 | }
32 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/protocol/Sender14.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.zabbix.protocol;
19 |
20 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
21 |
22 | /**
23 | * Old zabbix 1.1 sender protocol, XML based
24 | *
25 | * @author Andrea Dalle Vacche
26 | */
27 | public class Sender14 implements ISenderProtocol {
28 |
29 | @Override
30 | public boolean isMultiValueSupported() {
31 | return false;
32 | }
33 |
34 | @Override
35 | public String encodeItem(ZabbixItem item) {
36 | String result = "" + encode64(item.getHost()) + "";
37 | result += encode64(item.getKey());
38 | result += "";
39 | result += encode64(item.getValue());
40 | result += "";
41 | return result;
42 | }
43 |
44 | private String encode64(String input) {
45 | return new String(org.apache.commons.codec.binary.Base64.encodeBase64(input.getBytes()));
46 | }
47 |
48 | @Override
49 | public String encodeItems(ZabbixItem[] item) {
50 | throw new UnsupportedOperationException();
51 | }
52 |
53 | @Override
54 | public boolean isResponeOK(int readed, byte[] response) {
55 | if (readed != 2 || response[0] != 'O' || response[1] != 'K')
56 | return false;
57 | return true;
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/protocol/Sender18.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.zabbix.protocol;
19 |
20 | import java.io.UnsupportedEncodingException;
21 |
22 | import org.apache.commons.codec.binary.Base64;
23 |
24 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
25 |
26 | /**
27 | * Zabbix 1.8 sender protocol, JSON based
28 | *
29 | * @author Andrea Dalle Vacche
30 | */
31 | public class Sender18 implements ISenderProtocol {
32 | private static final String data = "";
33 |
34 | private static final String time = "";
35 |
36 | private static final String tail = "";
37 |
38 | @Override
39 | public boolean isMultiValueSupported() {
40 | return true;
41 | }
42 |
43 |
44 | /**
45 | * Encodes data for transmission to the server.
46 | *
47 | * This method encodes the data in the ASCII encoding, defaulting to
48 | * the platform default encoding if that is somehow unavailable.
49 | *
50 | * @param data
51 | * @return byte[] containing the encoded data
52 | */
53 | private byte[] encodeString(String data) {
54 | try {
55 | return data.getBytes("ASCII");
56 | } catch (UnsupportedEncodingException e) {
57 | return data.getBytes();
58 | }
59 | }
60 |
61 | private String base64Encode(String data) {
62 | return new String(Base64.encodeBase64(encodeString(data)));
63 | }
64 |
65 | private String buildJSonString(String host, String item, String value, String clock) {
66 | String head = "" + base64Encode(host) + "";
67 | final StringBuilder message = new StringBuilder(head);
68 |
69 | message.append(base64Encode(item));
70 | message.append(data);
71 |
72 | message.append(base64Encode(value == null ? "" : value));
73 | message.append(time);
74 | message.append(base64Encode(clock));
75 | message.append(tail);
76 |
77 | return message.toString();
78 | }
79 |
80 | @Override
81 | public String encodeItem(ZabbixItem item) {
82 | String payload = buildJSonString(item.getHost(), item.getKey(), item.getValue(),Long.toString(item.getClock()));
83 | return payload;
84 | }
85 |
86 | @Override
87 | public String encodeItems(ZabbixItem[] item) {
88 | // TODO Auto-generated method stub
89 | return null;
90 | }
91 |
92 | @Override
93 | public boolean isResponeOK(int readed, byte[] response) {
94 | if (readed != 2 || response[0] != 'O' || response[1] != 'K')
95 | return false;
96 | return true;
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/protocol/Sender32.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DBforBix.
3 | *
4 | * DBforBix is free software: you can redistribute it and/or modify it under the
5 | * terms of the GNU General Public License as published by the Free Software
6 | * Foundation, either version 3 of the License, or (at your option) any later
7 | * version.
8 | *
9 | * DBforBix is distributed in the hope that it will be useful, but WITHOUT ANY
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 | * details.
13 | *
14 | * You should have received a copy of the GNU General Public License along with
15 | * DBforBix. If not, see .
16 | */
17 |
18 | package com.smartmarmot.dbforbix.zabbix.protocol;
19 |
20 | import com.smartmarmot.dbforbix.zabbix.ZabbixItem;
21 |
22 | /**
23 | * Zabbix 3.2 Proxy sender protocol, JSON based
24 | *
25 | * @author Vagabondan
26 | */
27 | public class Sender32 implements ISenderProtocol {
28 |
29 |
30 |
31 | /**
32 | *
33 | * discovery data
34 | */
35 | private static final String ZBX_PROTO_VALUE_HISTORY_DATA="history data";
36 | //private static final String ZBX_PROTO_VALUE_DISCOVERY_DATA ="discovery data";
37 | //private static final String ZBX_PROTO_VALUE_AUTO_REGISTRATION_DATA="auto registration";
38 |
39 | //DBforBix
40 |
41 |
42 | //private static final String data = "";
43 |
44 | //private static final String time = "";
45 |
46 | //private static final String tail = "";
47 |
48 | @Override
49 | public boolean isMultiValueSupported() {
50 | return true;
51 | }
52 |
53 |
54 | private String escapeSpecialChars(String string) {
55 | String result=string;
56 | if(null!=result){
57 | result=result.replace("\\", "\\\\");//has to be the first one
58 | result=result.replace("\n", "\\n");
59 | result=result.replace("\"", "\\\"");
60 | result=result.replace("\b", "\\b");
61 | result=result.replace("\f", "\\f");
62 | result=result.replace("\r", "\\r");
63 | result=result.replace("\t", "\\t");
64 | }
65 | return result;
66 | }
67 |
68 | private String openJson(String type, String proxyName) {
69 | //{"request":(ZBX_PROTO_VALUE_HISTORY_DATA|ZBX_PROTO_VALUE_DISCOVERY_DATA|ZBX_PROTO_VALUE_AUTO_REGISTRATION_DATA),
70 | // "host":DBforBix,
71 | // "data":[{"host":HOST_NAME,"key":KEY,"clock":CLOCK,"ns":NS},{},...],
72 | // "clock":UNIXTIMESTAMP,
73 | // "ns":NS}
74 | String str=new String("{\"request\":\""+type+"\",\"host\":\""+proxyName+"\",\"data\":[");
75 | return str;
76 | }
77 |
78 | private String buildJsonData(ZabbixItem it) {
79 | //{"request":(ZBX_PROTO_VALUE_HISTORY_DATA|ZBX_PROTO_VALUE_DISCOVERY_DATA|ZBX_PROTO_VALUE_AUTO_REGISTRATION_DATA),
80 | // "host":DBforBix,
81 | // "data":[{"host":HOST_NAME,"key":KEY,"clock":CLOCK,"ns":NS},{},...],
82 | // "clock":UNIXTIMESTAMP,
83 | // "ns":NS}
84 | String str=new String();
85 | str+="{\"host\":\""+it.getHost()+"\",";
86 | str+="\"key\":\""+escapeSpecialChars(it.getKey())+"\",";
87 | str+="\"value\":\""+escapeSpecialChars(it.getValue())+"\",";
88 | /**
89 | * Error are passed in value while state is set to "not supported"
90 | */
91 | if(ZabbixItem.ZBX_STATE_NOTSUPPORTED == it.getState())
92 | str+="\"state\":\""+it.getState()+"\",";
93 | str+="\"clock\":"+it.getClock()+",";
94 | str+="\"ns\":0}";
95 | str+=",";
96 | return str;
97 | }
98 |
99 | private String closeJson(String type) {
100 | //{"request":(ZBX_PROTO_VALUE_HISTORY_DATA|ZBX_PROTO_VALUE_DISCOVERY_DATA|ZBX_PROTO_VALUE_AUTO_REGISTRATION_DATA),
101 | // "host":DBforBix,
102 | // "data":[{"host":HOST_NAME,"key":KEY,"clock":CLOCK,"ns":NS},{},...],
103 | // "clock":UNIXTIMESTAMP,
104 | // "ns":NS}
105 | String str=new String();
106 | str+="]";
107 | Long zbxPrxclock = new Long(System.currentTimeMillis() / 1000L);
108 | str+=",\"clock\":\""+zbxPrxclock+"\",\"ns\":\"0\"";
109 | str+="}";
110 | return str;
111 | }
112 |
113 | @Override
114 | public String encodeItems(ZabbixItem[] items) {
115 | return encodeItems(items, ZBX_PROTO_VALUE_HISTORY_DATA);
116 | }
117 |
118 | private String encodeItems(ZabbixItem[] items, String type) {
119 | String result=new String();
120 | String proxyName=items.length>0?items[0].getConfigurationElement().getZabbixServer().getProxy():"none";
121 | result+=openJson(type,proxyName);
122 | for(ZabbixItem it:items){
123 | result+=buildJsonData(it);
124 | }
125 | result=result.substring(0,result.length()-1);//delete last comma
126 | result+=closeJson(type);
127 | return result;
128 | }
129 |
130 | @Override
131 | public boolean isResponeOK(int readed, byte[] response) {
132 | if (readed < 0 )
133 | return false;
134 | return true;
135 | }
136 |
137 |
138 | @Override
139 | public String encodeItem(ZabbixItem item) {
140 | String result=null;
141 | ZabbixItem[] items=new ZabbixItem[1];
142 | items[0]=item;
143 | result = encodeItems(items,ZBX_PROTO_VALUE_HISTORY_DATA);
144 | return result;
145 | }
146 |
147 | }
148 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/protocol/json/Data.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.zabbix.protocol.json;
2 |
3 | class Data {
4 |
5 | private String host;
6 | private String key;
7 | private Object value;
8 | private Long clock;
9 |
10 | public Data() {}
11 |
12 | public Data(String host, String key, Object value) {
13 | super();
14 | this.host = host;
15 | this.key = key;
16 | this.value = value;
17 | this.clock = null;
18 | }
19 |
20 | public Data(String host, String key, Object value, Long clock) {
21 | super();
22 | this.host = host;
23 | this.key = key;
24 | this.value = value;
25 | this.clock = clock;
26 | }
27 |
28 | public String getHost() {
29 | return host;
30 | }
31 |
32 | public void setHost(String host) {
33 | this.host = host;
34 | }
35 |
36 | public String getKey() {
37 | return key;
38 | }
39 |
40 | public void setKey(String key) {
41 | this.key = key;
42 | }
43 |
44 | public Object getValue() {
45 | return value;
46 | }
47 |
48 | public void setValue(Object value) {
49 | this.value = value;
50 | }
51 |
52 | public Long getClock() {
53 | return clock;
54 | }
55 |
56 | public void setClock(Long clock) {
57 | this.clock = clock;
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/protocol/json/Request.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.zabbix.protocol.json;
2 |
3 | public class Request {
4 | private String request = "sender data";
5 | private Data[] data;
6 | private Long clock;
7 |
8 | public String getRequest() {
9 | return request;
10 | }
11 |
12 | public void setRequest(String request) {
13 | this.request = request;
14 | }
15 |
16 | public Data[] getData() {
17 | return data;
18 | }
19 |
20 | public void setData(Data[] data) {
21 | this.data = data;
22 | }
23 |
24 | public Long getClock() {
25 | return clock;
26 | }
27 |
28 | public void setClock(Long clock) {
29 | this.clock = clock;
30 | }
31 |
32 | }
--------------------------------------------------------------------------------
/src/com/smartmarmot/dbforbix/zabbix/protocol/json/Response.java:
--------------------------------------------------------------------------------
1 | package com.smartmarmot.dbforbix.zabbix.protocol.json;
2 |
3 |
4 | import java.io.Serializable;
5 |
6 | public class Response implements Serializable {
7 | private static final long serialVersionUID = 4944927703885182914L;
8 |
9 | private String response;
10 | private String info;
11 |
12 | public Response() {
13 | }
14 |
15 | public String getResponse() {
16 | return response;
17 | }
18 |
19 | public void setResponse(String response) {
20 | this.response = response;
21 | }
22 |
23 | public String getInfo() {
24 | return info;
25 | }
26 |
27 | public void setInfo(String info) {
28 | this.info = info;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------