config.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package eyelib::config;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use strict;
  8. use English;
  9. use FindBin '$Bin';
  10. use lib "/opt/Eye/scripts";
  11. use base 'Exporter';
  12. use vars qw(@EXPORT @ISA);
  13. use Config::Tiny;
  14. use File::Basename;
  15. use Data::Dumper;
  16. @ISA = qw(Exporter);
  17. @EXPORT = qw(
  18. $HOME_DIR
  19. @FN
  20. $MY_NAME
  21. $SPID
  22. $LOG_DIR
  23. $LOG_COMMON
  24. $LOG
  25. $LOG_ERR
  26. $LOG_DEBUG
  27. $DHCPD_CONF
  28. $BEGIN_STR
  29. $END_STR
  30. $WARN_MSG
  31. $WAIT_TIME
  32. $MIN_SLEEP
  33. $MAX_SLEEP
  34. $admin_email
  35. $sender_email
  36. $send_email
  37. $HOSTNAME
  38. $debug
  39. $log_enable
  40. $log_level
  41. $W_INFO
  42. $W_ERROR
  43. $W_DEBUG
  44. $DBHOST
  45. $DBNAME
  46. $DBUSER
  47. $DBPASS
  48. $domain_auth
  49. $winexe
  50. $fping
  51. $log_owner_user
  52. $log_owner_group
  53. $use_smsd
  54. $smsaero_wait
  55. $smsd_group
  56. $smsd_user
  57. $def_timeout
  58. $parallel_process_count
  59. $save_detail
  60. $add_unknown_user
  61. $router_ip
  62. $dns_server
  63. $dhcp_server
  64. $snmp_default_version
  65. $snmp_default_community
  66. $KB
  67. $office_networks
  68. $hotspot_networks
  69. $all_networks
  70. @office_network_list
  71. @hotspot_network_list
  72. @all_network_list
  73. $dhcp_pool
  74. $history
  75. $history_dhcp
  76. $router_login
  77. $router_password
  78. $router_port
  79. $org_name
  80. $domain_name
  81. $connections_history
  82. $dbh
  83. $urgent_sync
  84. $default_user_ou_id
  85. $default_hotspot_ou_id
  86. $ignore_hotspot_dhcp_log
  87. $ignore_update_dhcp_event
  88. $update_hostname_from_dhcp
  89. @subnets
  90. %subnets_ref
  91. $history_log_day
  92. $history_syslog_day
  93. $history_trafstat_day
  94. $free_networks
  95. $vpn_networks
  96. @free_network_list
  97. @vpn_network_list
  98. %config_ref
  99. %switch_auth
  100. $last_refresh_config
  101. $tftp_dir
  102. $tftp_server
  103. $cpu_count
  104. );
  105. BEGIN
  106. {
  107. our $HOME_DIR = '/opt/Eye/scripts';
  108. my $config_file = $HOME_DIR."/cfg/config";
  109. if (! -e "$config_file") { die "Config $config_file not found!"; }
  110. my $Config = Config::Tiny->new;
  111. $Config = Config::Tiny->read($config_file, 'utf8' );
  112. our %config_ref;
  113. ### current script pathname
  114. our @FN=split("/",$0);
  115. ### script pid file name
  116. $config_ref{my_name}=$FN[-1];
  117. $config_ref{pid_dir} ='/run';
  118. #for run as root - use /run dir for pid files
  119. if ($> > 0) {
  120. $config_ref{pid_dir}=$HOME_DIR.'/run';
  121. }
  122. $config_ref{pid_file} = $config_ref{pid_dir}."/".$FN[-1];
  123. $config_ref{log_dir} = $Config->{_}->{log_dir} || $HOME_DIR.'/log';
  124. $config_ref{log_common} = $config_ref{log_dir}."/$FN[-1].log";
  125. $config_ref{dhcpd_conf} = $Config->{_}->{dhcpd_conf} || "/etc/dnsmasq.d";
  126. $config_ref{DBTYPE} = $Config->{_}->{DBTYPE} || 'mysql';
  127. $config_ref{DBHOST} = $Config->{_}->{DBSERVER} || '127.0.0.1';
  128. $config_ref{DBNAME} = $Config->{_}->{DBNAME} || "stat";
  129. $config_ref{DBUSER} = $Config->{_}->{DBUSER} || "rstat";
  130. $config_ref{DBPASS} = $Config->{_}->{DBPASS} || "rstat";
  131. $config_ref{domain_auth} = $Config->{_}->{domain_auth} || 'Administrator%password';
  132. $config_ref{winexe} = $Config->{_}->{winexe} || '/usr/bin/winexe';
  133. $config_ref{fping} = $Config->{_}->{fping} || '/sbin/fping';
  134. $config_ref{log_owner_user} = $Config->{_}->{user} || 'eye';
  135. $config_ref{log_owner_group}= $Config->{_}->{group} || 'eye';
  136. $config_ref{nagios_dir}=$Config->{_}->{nagios_dir} || '/etc/nagios4';
  137. $config_ref{nagios_dir}=~s/\/$//;
  138. $config_ref{nagios_cmd}=$Config->{_}->{nagios_cmd} || '/var/spool/nagios/cmd/nagios.cmd';
  139. $config_ref{nagios_event_socket}=$Config->{_}->{nagios_event_socket} || '/var/spool/nagios/hoststate.socket';
  140. $config_ref{encryption_key}=$Config->{_}->{encryption_key} || '!!!CHANGE_ME!!!';
  141. $config_ref{encryption_iv}=$Config->{_}->{encryption_iv} || '123456782345';
  142. our $MY_NAME=$FN[-1];
  143. our $SPID=$config_ref{pid_file};
  144. #iptables log
  145. our $LOG_DIR = $config_ref{log_dir};
  146. our $LOG_COMMON = "$LOG_DIR/$FN[-1].log";
  147. our $LOG = $LOG_COMMON;
  148. our $LOG_ERR = $LOG_COMMON;
  149. our $LOG_DEBUG = $LOG_COMMON;
  150. our $DHCPD_CONF = $Config->{_}->{dhcpd_conf} || "/etc/dnsmasq.d";
  151. our $BEGIN_STR ="================= Start transaction ========================";
  152. our $END_STR ="================= Stop transaction ========================";
  153. our $WARN_MSG ="# DYNAMIC GENERATED FILE\n# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN\n";
  154. ### timeout for wait remove lock before exit
  155. our $WAIT_TIME =600;
  156. our $MIN_SLEEP =5;
  157. our $MAX_SLEEP =30;
  158. ### mail options
  159. our $admin_email;
  160. our $sender_email;
  161. our $send_email = 0;
  162. my $HOSTNAME1=`hostname`;
  163. chomp($HOSTNAME1);
  164. our $HOSTNAME=$HOSTNAME1;
  165. ### debug
  166. our $debug=0;
  167. our $log_enable = 1;
  168. our $log_level = 2;
  169. our $W_INFO = 0;
  170. our $W_ERROR = 1;
  171. our $W_DEBUG = 2;
  172. our $DBHOST = $config_ref{DBHOST};
  173. our $DBNAME = $config_ref{DBNAME};
  174. our $DBUSER = $config_ref{DBUSER};
  175. our $DBPASS = $config_ref{DBPASS};
  176. our $domain_auth = $config_ref{domain_auth};
  177. our $winexe = $config_ref{winexe};
  178. our $fping = $config_ref{fping};
  179. our @subnets=();
  180. our $history_log_day;
  181. our $history_syslog_day;
  182. our $history_trafstat_day;
  183. our $log_owner_user = $config_ref{log_owner_user};
  184. our $log_owner_group = $config_ref{log_owner_group};
  185. ################################################################
  186. our $def_timeout = 90;
  187. our $parallel_process_count = 10;
  188. our $cpu_count = 1;
  189. ################## DB options ##################################
  190. our $save_detail;
  191. our $add_unknown_user;
  192. our $router_ip;
  193. our $dns_server;
  194. our $dhcp_server;
  195. our $snmp_default_version;
  196. our $snmp_default_community;
  197. our $KB;
  198. our $office_networks;
  199. our $hotspot_networks;
  200. our $all_networks;
  201. our @office_network_list;
  202. our @hotspot_network_list;
  203. our @all_network_list;
  204. our $free_networks;
  205. our $vpn_networks;
  206. our @free_network_list;
  207. our @vpn_network_list;
  208. our $dhcp_pool;
  209. our $default_user_ou_id;
  210. our $default_hotspot_ou_id;
  211. our $history;
  212. our $history_dhcp;
  213. our $router_login;
  214. our $router_password;
  215. our $router_port;
  216. our $org_name;
  217. our $domain_name;
  218. our $connections_history;
  219. our $dbh;
  220. our $urgent_sync = 0;
  221. our $tftp_dir=$Config->{_}->{tftp_dir} || '/var/lib/tftpboot';
  222. our $tftp_server=$Config->{_}->{tftp_server} || '';
  223. our $last_refresh_config = time();
  224. our %switch_auth = (
  225. '8'=>{'vendor'=>'Allied Telesis','enable'=>'en','proto'=>'essh','port'=>'22','login'=> '(login|User Name):','password'=>'Password:','prompt'=>qr/(\010\013){0,5}(([-\w]+|[-\w(config)+])\#|[-\w]+\>)/},
  226. '3'=>{'vendor'=>'Huawei','proto'=>'essh','port'=>'22','enable'=>'system-view','login'=> 'login as:','password'=>'Password: ','prompt'=>qr/(\<.*\>|\[.*\])/},
  227. '16'=>{'vendor'=>'Cisco','proto'=>'ssh','port'=>'22','enable'=>'en','login'=> 'Username:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
  228. '5'=>{'vendor'=>'Raisecom','proto'=>'telnet','port'=>'23','enable'=>'en','login'=> 'Login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
  229. '6'=>{'vendor'=>'SNR','proto'=>'telnet','port'=>'23','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
  230. '7'=>{'vendor'=>'Dlink','proto'=>'telnet','port'=>'23','login'=> 'UserName:','password'=>'PassWord:','prompt'=>qr/[-\w]+\#$/},
  231. #'15'=>{'vendor'=>'HP','proto'=>'telnet','port'=>'23','enable'=>'system-view','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
  232. '2'=>{'vendor'=>'Eltex','proto'=>'telnet','port'=>'23','login'=> 'User Name:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
  233. '17'=>{'vendor'=>'Maipu','proto'=>'telnet','port'=>'23','login'=> 'login:','password'=>'password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
  234. '4'=>{'vendor'=>'Zyxel','proto'=>'telnet','port'=>'23','login'=> 'User name:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+]|[-\w(config-interface)+])\#/},
  235. '38'=>{'vendor'=>'Qtech','proto'=>'telnet','port'=>'23','enable'=>'en','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
  236. '9'=>{'vendor'=>'Mikrotik','proto'=>'ssh','port'=>'22','login'=> 'login as:','password'=>'password:','prompt'=>qr/\[[-\w]+\@[-\w]+\]\s+\>/},
  237. '39'=>{'vendor'=>'Extreme','proto'=>'telnet','port'=>'23','login'=> 'login:','password'=>'password:','prompt'=>qr/[-\w]+\s\#\s/},
  238. );
  239. mkdir $LOG_DIR unless (-d $LOG_DIR);
  240. mkdir $config_ref{pid_dir} unless (-d $config_ref{pid_dir});
  241. my @cpu_list = `grep ^processor /proc/cpuinfo`;
  242. $cpu_count = scalar @cpu_list;
  243. 1;
  244. }