install-eye.sh 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. #!/bin/bash
  2. # Eye Installation Script for ALT Linux/Debian/Ubuntu with PostgreSQL support
  3. # Version: 2.1
  4. set -e
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Output functions
  12. print_info() {
  13. echo -e "${GREEN}[INFO]${NC} $1"
  14. }
  15. print_warn() {
  16. echo -e "${YELLOW}[WARN]${NC} $1"
  17. }
  18. print_error() {
  19. echo -e "${RED}[ERROR]${NC} $1"
  20. }
  21. print_step() {
  22. echo -e "${BLUE}=== $1 ===${NC}"
  23. }
  24. # Check for root privileges
  25. check_root() {
  26. if [[ $EUID -ne 0 ]]; then
  27. print_error "This script must be run as root"
  28. print_error "Use: sudo $0"
  29. exit 1
  30. fi
  31. }
  32. # Detect distribution and package manager
  33. detect_distro() {
  34. if [[ -f /etc/os-release ]]; then
  35. . /etc/os-release
  36. OS_ID=$ID
  37. OS_VERSION=$VERSION_ID
  38. OS_NAME=$NAME
  39. case $OS_ID in
  40. altlinux)
  41. PACKAGE_MANAGER="apt-get"
  42. SERVICE_MANAGER="systemctl"
  43. OS_FAMILY="alt"
  44. print_info "Detected ALT Linux $OS_VERSION"
  45. ;;
  46. debian)
  47. PACKAGE_MANAGER="apt"
  48. SERVICE_MANAGER="systemctl"
  49. OS_FAMILY="debian"
  50. print_info "Detected Debian $OS_VERSION"
  51. ;;
  52. ubuntu)
  53. PACKAGE_MANAGER="apt"
  54. SERVICE_MANAGER="systemctl"
  55. OS_FAMILY="debian"
  56. print_info "Detected Ubuntu $OS_VERSION"
  57. ;;
  58. *)
  59. print_error "Unsupported distribution: $OS_ID"
  60. print_error "Supported: ALT Linux, Debian, Ubuntu"
  61. exit 1
  62. ;;
  63. esac
  64. else
  65. print_error "Failed to detect distribution"
  66. exit 1
  67. fi
  68. }
  69. select_language_with_auto() {
  70. print_step "Select Installation Language"
  71. # Проверка автоматического режима
  72. if [[ "$AUTO_MODE" == "true" ]]; then
  73. EYE_LANG="english"
  74. EYE_LANG_SHORT="en"
  75. print_info "Auto mode: English language selected by default"
  76. return 0
  77. fi
  78. echo "Available languages:"
  79. echo "1) English"
  80. echo "2) Russian (default)"
  81. echo ""
  82. while true; do
  83. read -p "Select language (1 or 2) [2]: " lang_choice
  84. # Если пустой ввод - по умолчанию английский
  85. if [[ -z "$lang_choice" ]]; then
  86. lang_choice="2"
  87. fi
  88. # Обработка ввода (приводим к нижнему регистру)
  89. lang_choice_lower=$(echo "$lang_choice" | tr '[:upper:]' '[:lower:]')
  90. case $lang_choice_lower in
  91. 1|english|en|eng|анг|английский)
  92. EYE_LANG="english"
  93. EYE_LANG_SHORT="en"
  94. print_info "Selected English language"
  95. break
  96. ;;
  97. 2|russian|ru|rus|ру|русский)
  98. EYE_LANG="russian"
  99. EYE_LANG_SHORT="ru"
  100. print_info "Selected Russian language (Русский)"
  101. break
  102. ;;
  103. *)
  104. print_error "Invalid choice: '$lang_choice'"
  105. print_warn "Available options: 1 (English), 2 (Russian)"
  106. print_warn "You can also type: english, en, russian, ru"
  107. ;;
  108. esac
  109. done
  110. }
  111. # Ask user for database type
  112. select_database_type() {
  113. print_step "Select Database Type"
  114. echo "Available database types:"
  115. echo "1) MySQL/MariaDB (default)"
  116. echo "2) PostgreSQL"
  117. echo ""
  118. read -p "Select database type (1 or 2) [1]: " db_choice
  119. case $db_choice in
  120. 2|postgres|postgresql|pgsql)
  121. DB_TYPE="postgresql"
  122. print_info "Selected PostgreSQL"
  123. ;;
  124. *)
  125. DB_TYPE="mysql"
  126. print_info "Selected MySQL/MariaDB"
  127. ;;
  128. esac
  129. }
  130. # Function for remote database configuration
  131. configure_remote_database() {
  132. echo ""
  133. echo "Remote Database Configuration"
  134. echo "============================="
  135. select_database_type
  136. read -p "Database server IP address: " DB_HOST
  137. # read -p "Database port [default]: " DB_PORT
  138. read -p "Database name: " DB_NAME
  139. read -p "Database username: " DB_USER
  140. read -sp "Database password: " DB_PASS
  141. echo ""
  142. # Set defaults if empty
  143. [[ -z "$DB_PORT" ]] && DB_PORT="3306"
  144. [[ "$DB_TYPE" == "postgresql" ]] && [[ "$DB_PORT" == "3306" ]] && DB_PORT="5432"
  145. echo "Database configuration saved:"
  146. echo " Type: $DB_TYPE"
  147. echo " Host: $DB_HOST:$DB_PORT"
  148. echo " Name: $DB_NAME"
  149. echo " User: $DB_USER"
  150. }
  151. # Function for installation type selection
  152. select_installation_type() {
  153. echo "Select installation type:"
  154. echo "1. Web interface + network backend"
  155. echo "2. Web interface only"
  156. echo "3. Network backend only"
  157. echo ""
  158. read -p "Enter selection number [1]: " install_type
  159. case $install_type in
  160. 1)
  161. INSTALL_TYPE="full"
  162. echo "Selected: Web interface + network backend"
  163. # Ask about database
  164. read -p "Install database locally? (y/n) [y]: " install_db
  165. if [[ -z "$install_db" || "$install_db" =~ ^[Yy]$ ]]; then
  166. DB_INSTALL="local"
  167. echo "Local database will be installed"
  168. select_database_type
  169. else
  170. DB_INSTALL="remote"
  171. echo "Remote database configuration"
  172. configure_remote_database
  173. fi
  174. ;;
  175. 2)
  176. INSTALL_TYPE="web"
  177. echo "Selected: Web interface only"
  178. DB_INSTALL="remote"
  179. configure_remote_database
  180. ;;
  181. 3)
  182. INSTALL_TYPE="backend"
  183. echo "Selected: Network backend only"
  184. DB_INSTALL="remote"
  185. configure_remote_database
  186. ;;
  187. *)
  188. INSTALL_TYPE="full"
  189. echo "Default selected: Web interface + network backend"
  190. DB_INSTALL="local"
  191. echo "Local database will be installed"
  192. select_database_type
  193. ;;
  194. esac
  195. }
  196. # Install dependencies for ALT Linux
  197. install_deps_altlinux() {
  198. print_step "Installing dependencies for ALT Linux"
  199. # Update repositories
  200. apt-get update
  201. # General utilities
  202. apt-get install -y git xxd wget fping hwdata rsync
  203. # Database installation based on selected type
  204. if [[ "$DB_TYPE" == "postgresql" ]]; then
  205. apt-get install -y postgresql17 postgresql17-server postgresql17-contrib postgresql17-perl
  206. else
  207. apt-get install -y mariadb-server mariadb-client
  208. fi
  209. # Web server and PHP
  210. if [[ "$DB_TYPE" == "postgresql" ]]; then
  211. apt-get install -y apache2 \
  212. php8.2 php8.2-pgsql php8.2-pdo_pgsql php8.2-intl php8.2-mbstring \
  213. pear-Mail php8.2-snmp php8.2-zip \
  214. php8.2-fpm-fcgi apache2-mod_fcgid
  215. else
  216. apt-get install -y apache2 \
  217. php8.2 php8.2-mysqlnd php8.2-intl php8.2-mbstring \
  218. pear-Mail php8.2-snmp php8.2-zip \
  219. php8.2-pgsql php8.2-mysqlnd php8.2-pdo_mysql php8.2-mysqlnd-mysqli \
  220. php8.2-fpm-fcgi apache2-mod_fcgid
  221. fi
  222. # Perl modules
  223. if [[ "$DB_TYPE" == "postgresql" ]]; then
  224. apt-get install -y perl perl-Net-Patricia perl-NetAddr-IP \
  225. perl-Config-Tiny perl-Net-DNS perl-DateTime perl-Net-Ping \
  226. perl-Net-Netmask perl-Text-Iconv perl-Net-SNMP \
  227. perl-Net-Telnet perl-DBI perl-DBD-Pg \
  228. perl-Parallel-ForkManager perl-Proc-Daemon \
  229. perl-DateTime-Format-DateParse \
  230. perl-Net-OpenSSH perl-File-Tail perl-Crypt-Rijndael \
  231. perl-Crypt-CBC perl-CryptX perl-Crypt-DES \
  232. perl-File-Path-Tiny perl-Expect \
  233. perl-Proc-ProcessTable
  234. else
  235. apt-get install -y perl perl-Net-Patricia perl-NetAddr-IP \
  236. perl-Config-Tiny perl-Net-DNS perl-DateTime perl-Net-Ping \
  237. perl-Net-Netmask perl-Text-Iconv perl-Net-SNMP \
  238. perl-Net-Telnet perl-DBI perl-DBD-mysql perl-DBD-Pg \
  239. perl-Parallel-ForkManager perl-Proc-Daemon \
  240. perl-DateTime-Format-DateParse \
  241. perl-Net-OpenSSH perl-File-Tail perl-Crypt-Rijndael \
  242. perl-Crypt-CBC perl-CryptX perl-Crypt-DES \
  243. perl-File-Path-Tiny perl-Expect \
  244. perl-Proc-ProcessTable
  245. fi
  246. # Additional services
  247. apt-get install -y dnsmasq syslog-ng syslog-ng-journal
  248. # Install pwgen if not present
  249. if ! command -v pwgen &> /dev/null; then
  250. apt-get install -y pwgen
  251. fi
  252. control fping public
  253. control ping public
  254. }
  255. # Install dependencies for Debian/Ubuntu
  256. install_deps_debian() {
  257. print_step "Installing dependencies for Debian/Ubuntu"
  258. # Update repositories
  259. apt-get update
  260. # General utilities
  261. apt-get install -y git xxd bsdmainutils pwgen wget fping ieee-data rsync
  262. # Database installation based on selected type
  263. if [[ "$DB_TYPE" == "postgresql" ]]; then
  264. apt-get install -y postgresql postgresql-client
  265. else
  266. apt-get install -y mariadb-server mariadb-client
  267. fi
  268. # Web server and PHP
  269. if [[ "$DB_TYPE" == "postgresql" ]]; then
  270. apt-get install -y apache2 \
  271. php php-pgsql php-bcmath php-intl php-mbstring \
  272. php-date php-mail php-snmp php-zip \
  273. php-db php-fpm libapache2-mod-fcgid
  274. else
  275. apt-get install -y apache2 \
  276. php php-mysql php-bcmath php-intl php-mbstring \
  277. php-date php-mail php-snmp php-zip \
  278. php-db php-pgsql php-fpm libapache2-mod-fcgid
  279. fi
  280. # Perl modules
  281. if [[ "$DB_TYPE" == "postgresql" ]]; then
  282. apt-get install -y perl libnet-patricia-perl libnetaddr-ip-perl \
  283. libconfig-tiny-perl libnet-dns-perl libdatetime-perl \
  284. libnet-netmask-perl libtext-iconv-perl libnet-snmp-perl \
  285. libnet-telnet-perl libdbi-perl \
  286. libparallel-forkmanager-perl libproc-daemon-perl \
  287. libdatetime-format-dateparse-perl \
  288. libnet-openssh-perl libfile-tail-perl libcrypt-rijndael-perl \
  289. libcrypt-cbc-perl libcryptx-perl libdbd-pg-perl \
  290. libfile-path-tiny-perl libexpect-perl libcrypt-des-perl
  291. else
  292. apt-get install -y perl libnet-patricia-perl libnetaddr-ip-perl \
  293. libconfig-tiny-perl libnet-dns-perl libdatetime-perl \
  294. libnet-netmask-perl libtext-iconv-perl libnet-snmp-perl \
  295. libnet-telnet-perl libdbi-perl libdbd-mysql-perl \
  296. libparallel-forkmanager-perl libproc-daemon-perl \
  297. libdatetime-format-dateparse-perl \
  298. libnet-openssh-perl libfile-tail-perl libcrypt-rijndael-perl \
  299. libcrypt-cbc-perl libcryptx-perl libdbd-pg-perl \
  300. libfile-path-tiny-perl libexpect-perl libcrypt-des-perl
  301. fi
  302. # Additional services
  303. apt-get install -y dnsmasq syslog-ng
  304. }
  305. # System update
  306. update_system() {
  307. print_step "Updating system"
  308. $PACKAGE_MANAGER update -y
  309. }
  310. # Install packages
  311. install_packages() {
  312. print_step "Installing packages"
  313. case $OS_FAMILY in
  314. alt)
  315. install_deps_altlinux
  316. ;;
  317. debian)
  318. install_deps_debian
  319. ;;
  320. esac
  321. }
  322. # Create user and group
  323. create_user_group() {
  324. print_step "Creating user and group"
  325. # Create group
  326. if ! getent group eye >/dev/null; then
  327. groupadd --system eye
  328. print_info "Group 'eye' created"
  329. else
  330. print_info "Group 'eye' already exists"
  331. fi
  332. # Create user
  333. if ! id -u eye >/dev/null 2>&1; then
  334. if [[ "$OS_FAMILY" == "alt" ]]; then
  335. # For ALT Linux
  336. useradd --system --shell /bin/bash --home-dir /opt/Eye \
  337. --gid eye --groups eye eye
  338. else
  339. # For Debian/Ubuntu
  340. adduser --system --disabled-password --disabled-login \
  341. --ingroup eye --home=/opt/Eye eye
  342. fi
  343. print_info "User 'eye' created"
  344. else
  345. print_info "User 'eye' already exists"
  346. fi
  347. # Create directory
  348. mkdir -p /opt/Eye
  349. chown eye:eye /opt/Eye
  350. chmod 770 /opt/Eye
  351. # Add nagios to eye group (if exists)
  352. if id -u nagios >/dev/null 2>&1; then
  353. usermod -a -G eye nagios
  354. print_info "User 'nagios' added to group 'eye'"
  355. fi
  356. }
  357. # Check and apply SNMP SHA512 patch
  358. apply_snmp_patch() {
  359. print_info "Checking for SNMPv3 SHA512 support..."
  360. # File paths
  361. USM_PATCH_FILE="/opt/Eye/docs/patches/sha512.patch"
  362. if [[ "$OS_FAMILY" == "alt" ]]; then
  363. USM_PATCH_FILE="/opt/Eye/docs/patches/sha512.alt.patch"
  364. fi
  365. USM_PM_FILE=""
  366. # Search for USM.pm in system
  367. local usm_paths=(
  368. "/usr/share/perl5/Net/SNMP/Security/USM.pm"
  369. "/usr/lib/perl5/vendor_perl/Net/SNMP/Security/USM.pm"
  370. "/usr/local/share/perl5/Net/SNMP/Security/USM.pm"
  371. )
  372. for path in "${usm_paths[@]}"; do
  373. if [[ -f "$path" ]]; then
  374. USM_PM_FILE="$path"
  375. print_info "Found USM.pm: $USM_PM_FILE"
  376. break
  377. fi
  378. done
  379. if [[ -z "$USM_PM_FILE" ]]; then
  380. print_warn "USM.pm file not found in system"
  381. return 1
  382. fi
  383. # Check if patch already applied
  384. if grep -q "AUTH_PROTOCOL_HMACSHA512" "$USM_PM_FILE"; then
  385. print_info "SHA512 patch already applied"
  386. return 0
  387. fi
  388. # Create backup
  389. cp "$USM_PM_FILE" "${USM_PM_FILE}.backup"
  390. print_info "Backup created: ${USM_PM_FILE}.backup"
  391. # Try to apply patch file
  392. local patch_applied=false
  393. if [[ -f "$USM_PATCH_FILE" ]]; then
  394. print_info "Attempting to apply patch from $USM_PATCH_FILE"
  395. # Check if patch can be applied
  396. if patch --dry-run -l -p1 -i "$USM_PATCH_FILE" -r /tmp/patch.rej "$USM_PM_FILE" 2>/dev/null; then
  397. # Apply patch
  398. if patch -l -p1 -i "$USM_PATCH_FILE" "$USM_PM_FILE" 2>/dev/null; then
  399. print_info "Patch successfully applied!"
  400. patch_applied=true
  401. else
  402. print_warn "Failed to apply patch (dry-run passed but actual application failed)"
  403. fi
  404. else
  405. print_warn "Patch cannot be applied automatically (version mismatch)"
  406. # Check differences
  407. print_info "Checking patch differences..."
  408. if [[ -f "/opt/Eye/docs/patches/USM.pm" ]]; then
  409. diff -u "$USM_PM_FILE" "/opt/Eye/docs/patches/USM.pm" > /tmp/usm.diff 2>/dev/null || true
  410. if [[ -s /tmp/usm.diff ]]; then
  411. print_warn "Differences found in USM.pm file"
  412. echo "Differences:"
  413. head -20 /tmp/usm.diff
  414. echo "..."
  415. fi
  416. fi
  417. fi
  418. fi
  419. # If patch not applied, ask user
  420. if [[ "$patch_applied" == false ]]; then
  421. echo ""
  422. print_warn "Automatic patch application failed"
  423. print_warn "Modification of USM.pm file required for SNMPv3 with SHA512 support"
  424. echo ""
  425. read -p "Do you need SNMPv3 SHA512 support? (y/n): " -n 1 -r
  426. echo
  427. if [[ $REPLY =~ ^[Yy]$ ]]; then
  428. # Try to replace the entire file
  429. if [[ -f "/opt/Eye/docs/patches/USM.pm" ]]; then
  430. print_info "Replacing USM.pm file entirely..."
  431. # Check version compatibility
  432. local original_ver=$(grep -i "version" "$USM_PM_FILE" | head -1)
  433. local patch_ver=$(grep -i "version" "/opt/Eye/docs/patches/USM.pm" | head -1)
  434. if [[ -n "$original_ver" && -n "$patch_ver" ]]; then
  435. print_info "Original file version: $original_ver"
  436. print_info "Patch version: $patch_ver"
  437. fi
  438. # Create additional backup
  439. cp "$USM_PM_FILE" "${USM_PM_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
  440. # Replace file
  441. cp -f "/opt/Eye/docs/patches/USM.pm" "$USM_PM_FILE"
  442. # Check if replacement successful
  443. if grep -q "SHA-512" "$USM_PM_FILE"; then
  444. print_info "USM.pm file successfully replaced, SHA512 support added"
  445. # Save replacement info
  446. echo "USM.pm file was replaced for SHA512 support" > "${USM_PM_FILE}.replaced"
  447. echo "Original file saved as: ${USM_PM_FILE}.backup" >> "${USM_PM_FILE}.replaced"
  448. echo "Replacement date: $(date)" >> "${USM_PM_FILE}.replaced"
  449. return 0
  450. else
  451. print_error "Failed to add SHA512 support after file replacement"
  452. # Restore from backup
  453. cp "${USM_PM_FILE}.backup" "$USM_PM_FILE"
  454. return 1
  455. fi
  456. else
  457. print_error "Patched USM.pm file not found in /opt/Eye/docs/patches/"
  458. return 1
  459. fi
  460. else
  461. print_info "SNMPv3 SHA512 support disabled"
  462. return 0
  463. fi
  464. fi
  465. return 0
  466. }
  467. # Download and copy source code
  468. install_source_code() {
  469. print_step "Installing Eye source code"
  470. # Create directory structure
  471. print_info "Creating directory structure..."
  472. mkdir -p /opt/Eye/scripts/cfg
  473. mkdir -p /opt/Eye/scripts/log
  474. mkdir -p /opt/Eye/html/cfg
  475. mkdir -p /opt/Eye/html/js
  476. mkdir -p /opt/Eye/docs
  477. chmod -R 755 /opt/Eye/html
  478. chmod -R 770 /opt/Eye/scripts/log
  479. chmod 750 /opt/Eye/scripts
  480. # Copy files
  481. print_info "Copying files..."
  482. cp -R scripts/ /opt/Eye/
  483. cp -R html/ /opt/Eye/
  484. cp -R docs/ /opt/Eye/
  485. # Set permissions
  486. chown -R eye:eye /opt/Eye
  487. # Apply SNMP SHA512 patch
  488. apply_snmp_patch
  489. }
  490. # Download additional scripts
  491. download_additional_scripts() {
  492. print_step "Downloading additional scripts"
  493. # Create directories
  494. mkdir -p /opt/Eye/html/js/jq
  495. mkdir -p /opt/Eye/html/js/select2
  496. mkdir -p /opt/Eye/html/js/jstree
  497. # Download jQuery
  498. print_info "Downloading jQuery..."
  499. if ! wget -q https://code.jquery.com/jquery-3.7.0.min.js \
  500. -O /opt/Eye/html/js/jq/jquery.min.js; then
  501. print_warn "Failed to download jQuery, trying alternative source..."
  502. wget -q https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js \
  503. -O /opt/Eye/html/js/jq/jquery.min.js || \
  504. print_error "Failed to download jQuery"
  505. fi
  506. # Download Select2
  507. print_info "Downloading Select2..."
  508. if wget -q https://github.com/select2/select2/archive/4.0.12.tar.gz -O 4.0.12.tar.gz; then
  509. tar -xzf 4.0.12.tar.gz -C /opt/Eye/html/js/select2/ \
  510. --strip-components=2 select2-4.0.12/dist 2>/dev/null || \
  511. tar -xzf 4.0.12.tar.gz -C /opt/Eye/html/js/select2/ \
  512. --strip-components=1 select2-4.0.12/dist 2>/dev/null
  513. rm -f 4.0.12.tar.gz
  514. else
  515. print_warn "Failed to download Select2"
  516. fi
  517. # Download jsTree
  518. print_info "Downloading jsTree..."
  519. if wget -q https://github.com/vakata/jstree/archive/3.3.12.tar.gz -O jstree.tar.gz; then
  520. tar -xzf jstree.tar.gz -C /opt/Eye/html/js/
  521. rsync -a /opt/Eye/html/js/jstree-3.3.12/dist/ /opt/Eye/html/js/jstree/
  522. rm -rf /opt/Eye/html/js/jstree-3.3.12
  523. rm -f jstree.tar.gz
  524. else
  525. print_warn "Failed to download jsTree"
  526. fi
  527. # Set permissions
  528. chown -R eye:eye /opt/Eye/html/js
  529. }
  530. # Configure MySQL
  531. setup_mysql() {
  532. print_step "Configuring MySQL"
  533. # Start and enable service
  534. $SERVICE_MANAGER enable mariadb 2>/dev/null || \
  535. $SERVICE_MANAGER enable mysql 2>/dev/null || true
  536. $SERVICE_MANAGER start mariadb 2>/dev/null || \
  537. $SERVICE_MANAGER start mysql 2>/dev/null || true
  538. # Check MySQL access
  539. if ! command -v mysql &> /dev/null; then
  540. print_error "MySQL client not installed"
  541. return 1
  542. fi
  543. MYSQL_OPT="-u root"
  544. # Check access without password
  545. if mysql -u root -e "SELECT 1;" 2>/dev/null; then
  546. print_info "MySQL accessible with empty password"
  547. echo ""
  548. print_warn "IMPORTANT: Need to set root password for MySQL!"
  549. print_warn "After installation run: mysql_secure_installation"
  550. echo ""
  551. else
  552. # Ask for password and create config file
  553. read -p "Enter MySQL root user password: " DB_ROOT_PASSWORD
  554. echo ""
  555. # Create temporary config file
  556. MYSQL_CNF_FILE="/tmp/mysql_root_eye.cnf"
  557. echo "[client]" > "$MYSQL_CNF_FILE"
  558. echo "user=root" >> "$MYSQL_CNF_FILE"
  559. echo "password=$DB_ROOT_PASSWORD" >> "$MYSQL_CNF_FILE"
  560. chmod 600 "$MYSQL_CNF_FILE"
  561. # Check connection
  562. if mysql --defaults-extra-file="$MYSQL_CNF_FILE" -e "SELECT 1;" &>/dev/null; then
  563. print_info "Successfully connected to MySQL"
  564. MYSQL_OPT="--defaults-extra-file=$MYSQL_CNF_FILE"
  565. else
  566. print_error "Incorrect MySQL root password"
  567. rm -f "$MYSQL_CNF_FILE"
  568. return 1
  569. fi
  570. fi
  571. read -p "Create database and user for Eye? (y/n): " -n 1 -r
  572. echo
  573. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  574. print_warn "Database creation skipped. Create manually:"
  575. print_warn " mysql -u root -p < ${SQL_CREATE_FILE}"
  576. print_warn " mysql -u root -p stat < ${SQL_DATA_FILE}"
  577. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  578. rm -f "$MYSQL_CNF_FILE"
  579. fi
  580. return 0
  581. fi
  582. # Generate password for stat user
  583. DB_PASSWORD=$(pwgen 16 1)
  584. MYSQL_PASSWORD=$DB_PASSWORD
  585. print_info "Importing database structure..."
  586. # Import main SQL file
  587. mysql $MYSQL_OPT < ${SQL_CREATE_FILE}
  588. if [[ $? -ne 0 ]]; then
  589. print_error "Error importing create_db.sql"
  590. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  591. rm -f "$MYSQL_CNF_FILE"
  592. fi
  593. return 1
  594. fi
  595. print_info "Database structure imported"
  596. # Import data
  597. print_info "Importing initial data..."
  598. mysql $MYSQL_OPT stat < ${SQL_DATA_FILE}
  599. if [[ $? -ne 0 ]]; then
  600. print_warn "Error importing data.sql (data may already exist)"
  601. else
  602. print_info "Initial data imported"
  603. fi
  604. # Create stat user
  605. print_info "Creating user 'stat'..."
  606. mysql $MYSQL_OPT <<EOF
  607. CREATE USER IF NOT EXISTS 'stat'@'localhost' IDENTIFIED BY '$DB_PASSWORD';
  608. GRANT ALL PRIVILEGES ON stat.* TO 'stat'@'localhost';
  609. FLUSH PRIVILEGES;
  610. EOF
  611. if [[ $? -ne 0 ]]; then
  612. print_error "Error creating user 'stat'"
  613. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  614. rm -f "$MYSQL_CNF_FILE"
  615. fi
  616. return 1
  617. fi
  618. print_info "User 'stat' successfully created"
  619. # Save password information
  620. echo "MySQL 'stat' user password: $DB_PASSWORD" > /root/eye_mysql_password.txt
  621. chmod 600 /root/eye_mysql_password.txt
  622. print_info "User 'stat' password: $DB_PASSWORD"
  623. print_warn "Password saved in /root/eye_mysql_password.txt"
  624. # Clean up temporary file if created
  625. if [[ -f "$MYSQL_CNF_FILE" ]]; then
  626. rm -f "$MYSQL_CNF_FILE"
  627. fi
  628. return 0
  629. }
  630. # Configure PostgreSQL
  631. setup_postgresql() {
  632. print_step "Configuring PostgreSQL"
  633. PGDATA="/var/lib/pgsql/data"
  634. if [[ "$OS_FAMILY" == "alt" ]]; then
  635. echo "root ALL=(ALL:ALL) NOPASSWD: ALL" >/etc/sudoers.d/root
  636. PGDATA="/var/lib/pgsql/data"
  637. if [ -z "$(ls -A $PGDATA 2>/dev/null)" ]; then
  638. /etc/init.d/postgresql initdb
  639. fi
  640. fi
  641. # Start and enable service
  642. $SERVICE_MANAGER enable postgresql
  643. $SERVICE_MANAGER start postgresql
  644. # Check PostgreSQL access
  645. if ! command -v psql &> /dev/null; then
  646. print_error "PostgreSQL client not installed"
  647. return 1
  648. fi
  649. # Switch to postgres user to execute commands
  650. read -p "Create database and user for Eye? (y/n): " -n 1 -r
  651. echo
  652. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  653. print_warn "Database creation skipped. Create manually as postgres user:"
  654. print_warn " sudo -u postgres psql -f ${SQL_CREATE_FILE}"
  655. print_warn " sudo -u postgres psql -d stat -f ${SQL_DATA_FILE}"
  656. return 0
  657. fi
  658. # Generate password for stat user
  659. DB_PASSWORD=$(pwgen 16 1)
  660. POSTGRES_PASSWORD=$DB_PASSWORD
  661. print_info "Importing database structure..."
  662. # Import main SQL file as postgres user
  663. if [[ "$OS_FAMILY" == "alt" ]]; then
  664. psql -U postgres -f ${SQL_CREATE_FILE}
  665. else
  666. sudo -u postgres psql -f ${SQL_CREATE_FILE}
  667. fi
  668. if [[ $? -ne 0 ]]; then
  669. print_error "Error importing create_db.sql"
  670. return 1
  671. fi
  672. print_info "Database structure imported"
  673. # Set password for stat user
  674. print_info "Setting password for user 'stat'..."
  675. if [[ "$OS_FAMILY" == "alt" ]]; then
  676. psql -U postgres -c "CREATE USER stat WITH PASSWORD '$DB_PASSWORD';"
  677. psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE stat TO stat;"
  678. else
  679. sudo -u postgres psql -c "CREATE USER stat WITH PASSWORD '$DB_PASSWORD';"
  680. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE stat TO stat;"
  681. fi
  682. # Import data
  683. print_info "Importing initial data..."
  684. if [[ "$OS_FAMILY" == "alt" ]]; then
  685. psql -U postgres -d stat -f ${SQL_DATA_FILE}
  686. else
  687. sudo -u postgres psql -d stat -f ${SQL_DATA_FILE}
  688. fi
  689. if [[ $? -ne 0 ]]; then
  690. print_warn "Error importing data.sql (data may already exist)"
  691. else
  692. print_info "Initial data imported"
  693. fi
  694. # Grant privileges on all tables to stat user
  695. print_info "Granting privileges on all tables to user 'stat'..."
  696. if [[ "$OS_FAMILY" == "alt" ]]; then
  697. psql -U postgres -d stat <<EOF
  698. GRANT ALL ON ALL TABLES IN SCHEMA public TO stat;
  699. GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO stat;
  700. GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO stat;
  701. ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO stat;
  702. ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO stat;
  703. ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO stat;
  704. EOF
  705. else
  706. sudo -u postgres psql -d stat <<EOF
  707. GRANT ALL ON ALL TABLES IN SCHEMA public TO stat;
  708. GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO stat;
  709. GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO stat;
  710. ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO stat;
  711. ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO stat;
  712. ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO stat;
  713. EOF
  714. fi
  715. # Configure PostgreSQL for MD5 authentication
  716. if [[ "$OS_FAMILY" == "alt" ]]; then
  717. local pg_hba_file="/var/lib/pgsql/data/pg_hba.conf"
  718. if [[ -f "$pg_hba_file" ]]; then
  719. # Backup original
  720. cp "$pg_hba_file" "${pg_hba_file}.backup"
  721. # Add local md5 authentication if not present
  722. if ! grep -q "local.*stat.*md5" "$pg_hba_file"; then
  723. echo "local stat stat scram-sha-256" >> "$pg_hba_file"
  724. print_info "Added MD5 authentication for stat user in pg_hba.conf"
  725. fi
  726. fi
  727. else
  728. local pg_hba_file="/etc/postgresql/$(ls /etc/postgresql/ | head -1)/main/pg_hba.conf"
  729. if [[ -f "$pg_hba_file" ]]; then
  730. # Backup original
  731. cp "$pg_hba_file" "${pg_hba_file}.backup"
  732. # Add local md5 authentication if not present
  733. if ! grep -q "local.*stat.*md5" "$pg_hba_file"; then
  734. echo "local stat stat scram-sha-256" >> "$pg_hba_file"
  735. print_info "Added MD5 authentication for stat user in pg_hba.conf"
  736. fi
  737. fi
  738. fi
  739. # Restart PostgreSQL to apply changes
  740. $SERVICE_MANAGER restart postgresql
  741. # Save password information
  742. echo "PostgreSQL 'stat' user password: $DB_PASSWORD" > /root/eye_postgres_password.txt
  743. chmod 600 /root/eye_postgres_password.txt
  744. print_info "User 'stat' password: $DB_PASSWORD"
  745. print_warn "Password saved in /root/eye_postgres_password.txt"
  746. return 0
  747. }
  748. # Configure database based on selected type
  749. setup_database() {
  750. # Выбор правильных SQL файлов для импорта данных
  751. if [[ "$DB_TYPE" == "mysql" ]]; then
  752. if [[ "$EYE_LANG" == "russian" && -d "/opt/Eye/docs/databases/mysql/ru" ]]; then
  753. SQL_DATA_FILE="/opt/Eye/docs/databases/mysql/ru/data.sql"
  754. SQL_CREATE_FILE="/opt/Eye/docs/databases/mysql/ru/create_db.sql"
  755. else
  756. SQL_DATA_FILE="/opt/Eye/docs/databases/mysql/en/data.sql"
  757. SQL_CREATE_FILE="/opt/Eye/docs/databases/mysql/en/create_db.sql"
  758. fi
  759. elif [[ "$DB_TYPE" == "postgresql" ]]; then
  760. if [[ "$EYE_LANG" == "russian" && -d "/opt/Eye/docs/databases/postgres/ru" ]]; then
  761. SQL_DATA_FILE="/opt/Eye/docs/databases/postgres/ru/data.sql"
  762. SQL_CREATE_FILE="/opt/Eye/docs/databases/postgres/ru/create_db.sql"
  763. else
  764. SQL_DATA_FILE="/opt/Eye/docs/databases/postgres/en/data.sql"
  765. SQL_CREATE_FILE="/opt/Eye/docs/databases/postgres/en/create_db.sql"
  766. fi
  767. fi
  768. print_info "Using SQL files for $EYE_LANG language"
  769. if [[ "$DB_TYPE" == "postgresql" ]]; then
  770. setup_postgresql
  771. else
  772. setup_mysql
  773. fi
  774. }
  775. # Configure configuration files
  776. setup_configs() {
  777. print_step "Configuring configuration files"
  778. # Copy configuration files
  779. if [[ -f "/opt/Eye/html/cfg/config.sample.php" ]]; then
  780. cp /opt/Eye/html/cfg/config.sample.php /opt/Eye/html/cfg/config.php
  781. fi
  782. if [[ -f "/opt/Eye/scripts/cfg/config.sample" ]]; then
  783. cp /opt/Eye/scripts/cfg/config.sample /opt/Eye/scripts/cfg/config
  784. fi
  785. # Generate encryption keys
  786. print_info "Generating encryption keys..."
  787. if command -v pwgen &> /dev/null; then
  788. ENC_PASSWORD=$(pwgen 16 1)
  789. else
  790. ENC_PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c16)
  791. fi
  792. ENC_VECTOR=$(tr -dc 0-9 </dev/urandom | head -c 16)
  793. # Configure config.php
  794. if [[ -f "/opt/Eye/html/cfg/config.sample.php" ]]; then
  795. cp /opt/Eye/html/cfg/config.sample.php /opt/Eye/html/cfg/config.php
  796. # Update database configuration based on type
  797. if [[ "$DB_TYPE" == "postgresql" ]]; then
  798. # PostgreSQL configuration
  799. if [[ -n "$POSTGRES_PASSWORD" ]]; then
  800. sed -i "s/define(\"DB_PASS\",\"[^\"]*\");/define(\"DB_PASS\",\"$POSTGRES_PASSWORD\");/" /opt/Eye/html/cfg/config.php
  801. fi
  802. sed -i "s/define(\"DB_TYPE\",\"[^\"]*\");/define(\"DB_TYPE\",\"postgresql\");/" /opt/Eye/html/cfg/config.php
  803. sed -i "s/define(\"DB_HOST\",\"[^\"]*\");/define(\"DB_HOST\",\"localhost\");/" /opt/Eye/html/cfg/config.php
  804. sed -i "s/define(\"DB_PORT\",\"[^\"]*\");/define(\"DB_PORT\",\"5432\");/" /opt/Eye/html/cfg/config.php
  805. sed -i "s/define(\"DB_NAME\",\"[^\"]*\");/define(\"DB_NAME\",\"stat\");/" /opt/Eye/html/cfg/config.php
  806. sed -i "s/define(\"DB_USER\",\"[^\"]*\");/define(\"DB_USER\",\"stat\");/" /opt/Eye/html/cfg/config.php
  807. else
  808. # MySQL configuration
  809. if [[ -n "$MYSQL_PASSWORD" ]]; then
  810. sed -i "s/define(\"DB_PASS\",\"[^\"]*\");/define(\"DB_PASS\",\"$MYSQL_PASSWORD\");/" /opt/Eye/html/cfg/config.php
  811. fi
  812. sed -i "s/define(\"DB_TYPE\",\"[^\"]*\");/define(\"DB_TYPE\",\"mysql\");/" /opt/Eye/html/cfg/config.php
  813. sed -i "s/define(\"DB_HOST\",\"[^\"]*\");/define(\"DB_HOST\",\"localhost\");/" /opt/Eye/html/cfg/config.php
  814. sed -i "s/define(\"DB_PORT\",\"[^\"]*\");/define(\"DB_PORT\",\"3306\");/" /opt/Eye/html/cfg/config.php
  815. sed -i "s/define(\"DB_NAME\",\"[^\"]*\");/define(\"DB_NAME\",\"stat\");/" /opt/Eye/html/cfg/config.php
  816. sed -i "s/define(\"DB_USER\",\"[^\"]*\");/define(\"DB_USER\",\"stat\");/" /opt/Eye/html/cfg/config.php
  817. fi
  818. # Update encryption key
  819. sed -i "s/ENCRYPTION_KEY\",\"[^\"]*\"/ENCRYPTION_KEY\",\"$ENC_PASSWORD\"/" /opt/Eye/html/cfg/config.php
  820. sed -i "s/ENCRYPTION_KEY','[^']*'/ENCRYPTION_KEY','$ENC_PASSWORD'/" /opt/Eye/html/cfg/config.php
  821. # Update initialization vector
  822. sed -i "s/ENCRYPTION_IV\",\"[^\"]*\"/ENCRYPTION_IV\",\"$ENC_VECTOR\"/" /opt/Eye/html/cfg/config.php
  823. sed -i "s/ENCRYPTION_IV','[^']*'/ENCRYPTION_IV','$ENC_VECTOR'/" /opt/Eye/html/cfg/config.php
  824. print_info "Configuration file config.php created from template"
  825. fi
  826. # Configure config for scripts
  827. if [[ -f "/opt/Eye/scripts/cfg/config.sample" ]]; then
  828. cp /opt/Eye/scripts/cfg/config.sample /opt/Eye/scripts/cfg/config
  829. # Update database configuration based on type
  830. if [[ "$DB_TYPE" == "postgresql" ]]; then
  831. # PostgreSQL configuration
  832. sed -i "s/^DBTYPE=.*/DBTYPE=postgresql/" /opt/Eye/scripts/cfg/config
  833. sed -i "s/DBTYPE=db_type/DBTYPE=postgresql/" /opt/Eye/scripts/cfg/config
  834. # Update database connection settings
  835. sed -i "s/^DBHOST=.*/DBHOST=localhost/" /opt/Eye/scripts/cfg/config
  836. sed -i "s/^DBPORT=.*/DBPORT=5432/" /opt/Eye/scripts/cfg/config
  837. if [[ -n "$POSTGRES_PASSWORD" ]]; then
  838. sed -i "s/^DBPASS=.*/DBPASS=$POSTGRES_PASSWORD/" /opt/Eye/scripts/cfg/config
  839. sed -i "s/DBPASS=db_password/DBPASS=$POSTGRES_PASSWORD/" /opt/Eye/scripts/cfg/config
  840. fi
  841. else
  842. # MySQL configuration
  843. sed -i "s/^DBTYPE=.*/DBTYPE=mysql/" /opt/Eye/scripts/cfg/config
  844. sed -i "s/DBTYPE=db_type/DBTYPE=mysql/" /opt/Eye/scripts/cfg/config
  845. # Update database connection settings
  846. sed -i "s/^DBHOST=.*/DBHOST=localhost/" /opt/Eye/scripts/cfg/config
  847. sed -i "s/^DBPORT=.*/DBPORT=3306/" /opt/Eye/scripts/cfg/config
  848. if [[ -n "$MYSQL_PASSWORD" ]]; then
  849. sed -i "s/^DBPASS=.*/DBPASS=$MYSQL_PASSWORD/" /opt/Eye/scripts/cfg/config
  850. sed -i "s/DBPASS=db_password/DBPASS=$MYSQL_PASSWORD/" /opt/Eye/scripts/cfg/config
  851. fi
  852. fi
  853. # Common settings
  854. sed -i "s/^DBNAME=.*/DBNAME=stat/" /opt/Eye/scripts/cfg/config
  855. sed -i "s/DBNAME=db_database/DBNAME=stat/" /opt/Eye/scripts/cfg/config
  856. sed -i "s/^DBUSER=.*/DBUSER=stat/" /opt/Eye/scripts/cfg/config
  857. sed -i "s/DBUSER=db_user/DBUSER=stat/" /opt/Eye/scripts/cfg/config
  858. # Update encryption key
  859. sed -i "s/^encryption_key=.*/encryption_key=$ENC_PASSWORD/" /opt/Eye/scripts/cfg/config
  860. sed -i "s/encryption_key=!!!CHANGE_ME!!!!/encryption_key=$ENC_PASSWORD/" /opt/Eye/scripts/cfg/config
  861. # Update initialization vector
  862. sed -i "s/^encryption_iv=.*/encryption_iv=$ENC_VECTOR/" /opt/Eye/scripts/cfg/config
  863. sed -i "s/encryption_iv=0123456789012345/encryption_iv=$ENC_VECTOR/" /opt/Eye/scripts/cfg/config
  864. print_info "Configuration file scripts/cfg/config created from template"
  865. fi
  866. # Set permissions
  867. chown -R eye:eye /opt/Eye/html/cfg /opt/Eye/scripts/cfg
  868. chmod 660 /opt/Eye/html/cfg/config.php /opt/Eye/scripts/cfg/config
  869. chmod 750 /opt/Eye/html/cfg /opt/Eye/scripts/cfg
  870. print_info "Encryption keys generated"
  871. print_info "Password: $ENC_PASSWORD"
  872. print_info "Vector: $ENC_VECTOR"
  873. }
  874. # Функция применения языковых настроек к конфигурации
  875. apply_language_settings() {
  876. print_info "Applying language settings: $EYE_LANG"
  877. # Настройка config.php
  878. if [[ -f "/opt/Eye/html/cfg/config.php" ]]; then
  879. if [[ "$EYE_LANG" == "russian" ]]; then
  880. # Установка русского языка
  881. sed -i "s/define(\"HTML_LANG\",\"english\");/define(\"HTML_LANG\",\"russian\");/g" /opt/Eye/html/cfg/config.php
  882. sed -i "s/setlocale(LC_ALL, 'en_US\.UTF-8');/setlocale(LC_ALL, 'ru_RU.UTF8');/g" /opt/Eye/html/cfg/config.php
  883. print_info "Web interface language set to Russian"
  884. else
  885. # Установка английского языка (по умолчанию)
  886. sed -i "s/define(\"HTML_LANG\",\"russian\");/define(\"HTML_LANG\",\"english\");/g" /opt/Eye/html/cfg/config.php
  887. sed -i "s/setlocale(LC_ALL, 'ru_RU\.UTF8');/setlocale(LC_ALL, 'en_US.UTF-8');/g" /opt/Eye/html/cfg/config.php
  888. print_info "Web interface language set to English"
  889. fi
  890. fi
  891. }
  892. # Configure Apache and PHP
  893. setup_apache_php() {
  894. print_step "Configuring Apache and PHP"
  895. # Determine PHP version
  896. PHP_VERSION=$(php -v 2>/dev/null | head -n1 | grep -oP '\d+\.\d+' || echo "8.1")
  897. # Configure PHP for all distributions
  898. if [[ "$OS_FAMILY" == "alt" ]]; then
  899. # ALT Linux
  900. PHP_INI="/etc/php/$PHP_VERSION/apache2/php.ini"
  901. APACHE_CONF_DIR="/etc/httpd2/conf"
  902. APACHE_SITES_DIR="$APACHE_CONF_DIR/sites-available"
  903. DEFAULT_CONF="$APACHE_SITES_DIR/000-default.conf"
  904. APACHE_USER="apache2"
  905. else
  906. # Debian/Ubuntu
  907. PHP_INI="/etc/php/$PHP_VERSION/apache2/php.ini"
  908. APACHE_CONF_DIR="/etc/apache2"
  909. APACHE_SITES_DIR="$APACHE_CONF_DIR/sites-available"
  910. DEFAULT_CONF="$APACHE_SITES_DIR/000-default.conf"
  911. APACHE_USER="www-data"
  912. fi
  913. # Configure Apache
  914. if [[ -f "/opt/Eye/docs/apache/000-default.conf" ]]; then
  915. print_info "Using prepared Apache template for ALT Linux"
  916. # Create directory if it doesn't exist
  917. mkdir -p "$APACHE_SITES_DIR"
  918. # Copy prepared config
  919. cp "/opt/Eye/docs/apache/000-default.conf" "$DEFAULT_CONF"
  920. # Enable site
  921. if [[ -f "$APACHE_CONF_DIR/sites-enabled/000-default.conf" ]]; then
  922. rm -f "$APACHE_CONF_DIR/sites-enabled/000-default.conf"
  923. ln -sf "$DEFAULT_CONF" "$APACHE_CONF_DIR/sites-enabled/000-default.conf"
  924. fi
  925. fi
  926. # Configure sudoers
  927. if [[ -f "/opt/Eye/docs/sudoers.d/www-data" ]]; then
  928. # Use prepared template, substituting correct user
  929. sed "s/www-data/eye/g" /opt/Eye/docs/sudoers.d/www-data > /etc/sudoers.d/eye
  930. chmod 440 /etc/sudoers.d/eye
  931. print_info "Sudoers file created from template"
  932. fi
  933. # Restart Apache
  934. if [[ "$OS_FAMILY" == "alt" ]]; then
  935. # ALT Linux uses httpd2
  936. APACHE_SERVICE="httpd2"
  937. else
  938. APACHE_SERVICE="apache2"
  939. fi
  940. # usermod -a -G eye $APACHE_USER
  941. if [[ "$OS_FAMILY" == "debian" ]]; then
  942. a2dismod php${PHP_VERSION} 2>/dev/null
  943. a2dismod mpm_prefork 2>/dev/null
  944. a2enmod mpm_event 2>/dev/null
  945. a2enconf php${PHP_VERSION}-fpm 2>/dev/null
  946. fi
  947. mkdir -p /var/log/php-fpm/
  948. a2enmod setenvif
  949. a2enmod proxy
  950. a2enmod proxy_fcgi
  951. print_info "Apache configured, sudoers user: $APACHE_USER"
  952. print_info "Apache service: $APACHE_SERVICE"
  953. # Configure php-fpm
  954. print_info "Configure php-fpm${PHP_VERSION}"
  955. if [[ -f "/opt/Eye/docs/php-fpm/eye.conf" ]]; then
  956. print_info "Using prepared php-fpm template"
  957. if [[ "$OS_FAMILY" == "alt" ]]; then
  958. cp "/opt/Eye/docs/php-fpm/eye.conf" /etc/fpm${PHP_VERSION}/php-fpm.d/
  959. else
  960. cp "/opt/Eye/docs/php-fpm/eye.conf" /etc/php/${PHP_VERSION}/fpm/pool.available/
  961. ln -sf "/etc/php/${PHP_VERSION}/fpm/pool.available/eye.conf" "/etc/php/${PHP_VERSION}/fpm/pool.d/eye.conf"
  962. fi
  963. fi
  964. $SERVICE_MANAGER enable "$APACHE_SERVICE"
  965. $SERVICE_MANAGER restart "$APACHE_SERVICE"
  966. $SERVICE_MANAGER enable php${PHP_VERSION}-fpm.service
  967. $SERVICE_MANAGER restart php${PHP_VERSION}-fpm.service
  968. # Check configuration
  969. if [[ "$OS_FAMILY" == "alt" ]]; then
  970. httpd2 -t 2>/dev/null && print_info "Apache (httpd2) configuration is valid" || print_warn "Check Apache configuration"
  971. else
  972. apache2ctl -t 2>/dev/null && print_info "Apache configuration is valid" || print_warn "Check Apache configuration"
  973. fi
  974. }
  975. # Configure cron and logrotate
  976. setup_cron_logrotate() {
  977. print_step "Configuring cron and logrotate"
  978. # Cron
  979. if [[ -f "/opt/Eye/docs/cron/stat" ]]; then
  980. cp /opt/Eye/docs/cron/stat /etc/cron.d/eye
  981. chmod 644 /etc/cron.d/eye
  982. print_info "Cron job added: /etc/cron.d/eye"
  983. fi
  984. # Logrotate
  985. if [[ -f "/opt/Eye/docs/logrotate/dnsmasq" ]]; then
  986. cp /opt/Eye/docs/logrotate/dnsmasq /etc/logrotate.d/dnsmasq-eye
  987. fi
  988. if [[ -f "/opt/Eye/docs/logrotate/scripts" ]]; then
  989. cp /opt/Eye/docs/logrotate/scripts /etc/logrotate.d/eye-scripts
  990. fi
  991. print_info "Cron and logrotate configuration completed"
  992. print_warn "Edit /etc/cron.d/eye to enable required scripts"
  993. }
  994. # Configure DHCP server (dnsmasq)
  995. setup_dhcp_server() {
  996. print_step "Configuring DHCP server"
  997. read -p "Configure DHCP server (dnsmasq)? (y/n): " -n 1 -r
  998. echo
  999. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  1000. return 0
  1001. fi
  1002. # Backup configuration
  1003. if [[ -f "/etc/dnsmasq.conf" ]]; then
  1004. cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
  1005. fi
  1006. # Copy configuration from Eye
  1007. if [[ -f "/opt/Eye/docs/addons/dnsmasq.conf" ]]; then
  1008. cat /opt/Eye/docs/addons/dnsmasq.conf > /etc/dnsmasq.conf
  1009. fi
  1010. # Copy systemd services
  1011. if [[ -f "/opt/Eye/docs/systemd/dhcp-log.service" ]]; then
  1012. cp /opt/Eye/docs/systemd/dhcp-log.service /etc/systemd/system/
  1013. fi
  1014. if [[ -f "/opt/Eye/docs/systemd/dhcp-log-truncate.service" ]]; then
  1015. cp /opt/Eye/docs/systemd/dhcp-log-truncate.service /etc/systemd/system/
  1016. fi
  1017. # Enable services
  1018. $SERVICE_MANAGER enable dnsmasq
  1019. $SERVICE_MANAGER start dnsmasq
  1020. print_info "DHCP server configured"
  1021. print_warn "Edit /etc/dnsmasq.conf for your network"
  1022. }
  1023. # Configure syslog-ng
  1024. setup_syslog() {
  1025. print_step "Configuring syslog-ng"
  1026. read -p "Configure remote log collection (syslog-ng)? (y/n): " -n 1 -r
  1027. echo
  1028. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  1029. return 0
  1030. fi
  1031. # Create backup of main config
  1032. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  1033. cp /etc/syslog-ng/syslog-ng.conf /etc/syslog-ng/syslog-ng.conf.backup
  1034. print_info "Backup created: /etc/syslog-ng/syslog-ng.conf.backup"
  1035. fi
  1036. # Copy additional config for Eye
  1037. if [[ -f "/opt/Eye/docs/syslog-ng/eye.conf" ]]; then
  1038. mkdir -p /etc/syslog-ng/conf.d
  1039. cp /opt/Eye/docs/syslog-ng/eye.conf /etc/syslog-ng/conf.d/eye.conf
  1040. # Check if conf.d inclusion already exists in main config
  1041. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  1042. if ! grep -q "@include.*conf\.d" /etc/syslog-ng/syslog-ng.conf && \
  1043. ! grep -q "include.*conf\.d" /etc/syslog-ng/syslog-ng.conf; then
  1044. # Add conf.d directory inclusion to end of file
  1045. echo "" >> /etc/syslog-ng/syslog-ng.conf
  1046. echo "# Include Eye monitoring configuration" >> /etc/syslog-ng/syslog-ng.conf
  1047. echo "@include \"/etc/syslog-ng/conf.d/*.conf\"" >> /etc/syslog-ng/syslog-ng.conf
  1048. print_info "Added conf.d directory inclusion to syslog-ng.conf"
  1049. fi
  1050. fi
  1051. print_info "Configuration file eye.conf copied to /etc/syslog-ng/conf.d/"
  1052. else
  1053. print_warn "eye.conf configuration file not found in /opt/Eye/docs/syslog-ng/"
  1054. fi
  1055. # options block
  1056. syslogng_options='options {
  1057. chain_hostnames(off);
  1058. flush_lines(0);
  1059. use_dns(no);
  1060. use_fqdn(no);
  1061. dns_cache(no);
  1062. owner("root");
  1063. group("adm");
  1064. perm(0640);
  1065. stats_freq(0);
  1066. time_reopen(10);
  1067. log_fifo_size(1000);
  1068. create_dirs(yes);
  1069. keep_hostname(no);
  1070. };'
  1071. # Check for options in main config
  1072. if [[ -f "/etc/syslog-ng/syslog-ng.conf" ]]; then
  1073. if ! grep -q "^options\s*{" /etc/syslog-ng/syslog-ng.conf; then
  1074. # Add options block if it doesn't exist
  1075. if grep -q "^@version:" /etc/syslog-ng/syslog-ng.conf; then
  1076. # Insert after @version: line
  1077. sed -i "/^@version:/a\\$syslogng_options" /etc/syslog-ng/syslog-ng.conf
  1078. else
  1079. # Insert at beginning of file
  1080. sed -i "1i\\$syslogng_options" /etc/syslog-ng/syslog-ng.conf
  1081. fi
  1082. print_info "Added options block to syslog-ng.conf"
  1083. else
  1084. # Check for required parameters in existing options block
  1085. local missing_params=()
  1086. if ! grep -q "time_reopen\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  1087. missing_params+=("time_reopen(10)")
  1088. fi
  1089. if ! grep -q "log_fifo_size\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  1090. missing_params+=("log_fifo_size(1000)")
  1091. fi
  1092. if ! grep -q "chain_hostnames\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  1093. missing_params+=("chain_hostnames(off)")
  1094. fi
  1095. if ! grep -q "create_dirs\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  1096. missing_params+=("create_dirs(yes)")
  1097. fi
  1098. if ! grep -q "keep_hostname\s*(.*)" /etc/syslog-ng/syslog-ng.conf; then
  1099. missing_params+=("keep_hostname(no)")
  1100. fi
  1101. # Add missing parameters
  1102. if [[ ${#missing_params[@]} -gt 0 ]]; then
  1103. # Find options block and add parameters to end of block
  1104. sed -i '/^options\s*{/,/^}/ {
  1105. /^}/ i\ '"$(IFS='; '; echo "${missing_params[*]}")"';
  1106. }' /etc/syslog-ng/syslog-ng.conf
  1107. print_info "Added parameters to options block: ${missing_params[*]}"
  1108. fi
  1109. fi
  1110. fi
  1111. # Copy systemd service for Eye log processing
  1112. if [[ -f "/opt/Eye/docs/systemd/syslog-stat.service" ]]; then
  1113. cp /opt/Eye/docs/systemd/syslog-stat.service /etc/systemd/system/
  1114. chmod 644 /etc/systemd/system/syslog-stat.service
  1115. print_info "syslog-stat service copied"
  1116. fi
  1117. # Create log directory if it doesn't exist
  1118. mkdir -p /opt/Eye/scripts/log
  1119. chown eye:eye /opt/Eye/scripts/log
  1120. chmod 770 /opt/Eye/scripts/log
  1121. # Enable and start services
  1122. $SERVICE_MANAGER daemon-reload
  1123. if $SERVICE_MANAGER enable syslog-ng; then
  1124. print_info "syslog-ng service enabled for autostart"
  1125. else
  1126. print_warn "Failed to enable syslog-ng for autostart"
  1127. fi
  1128. if $SERVICE_MANAGER restart syslog-ng; then
  1129. print_info "syslog-ng service restarted"
  1130. else
  1131. print_warn "Failed to restart syslog-ng"
  1132. fi
  1133. if [[ -f "/etc/systemd/system/syslog-stat.service" ]]; then
  1134. if $SERVICE_MANAGER enable syslog-stat; then
  1135. print_info "syslog-stat service enabled for autostart"
  1136. else
  1137. print_warn "Failed to enable syslog-stat for autostart"
  1138. fi
  1139. if $SERVICE_MANAGER start syslog-stat; then
  1140. print_info "syslog-stat service started"
  1141. else
  1142. print_warn "Failed to start syslog-stat"
  1143. fi
  1144. fi
  1145. # Check syslog-ng configuration
  1146. if command -v syslog-ng &> /dev/null; then
  1147. if syslog-ng --syntax-only; then
  1148. print_info "syslog-ng configuration is valid"
  1149. else
  1150. print_error "Error in syslog-ng configuration"
  1151. print_warn "Check files: /etc/syslog-ng/syslog-ng.conf and /etc/syslog-ng/conf.d/eye.conf"
  1152. fi
  1153. fi
  1154. print_info "syslog-ng configuration completed"
  1155. print_info "To receive logs from devices, configure them to send to IP: $(hostname -f)"
  1156. }
  1157. # Configure additional services
  1158. setup_additional_services() {
  1159. print_step "Configuring additional services"
  1160. # stat-sync service
  1161. if [[ -f "/opt/Eye/docs/systemd/stat-sync.service" ]]; then
  1162. cp /opt/Eye/docs/systemd/stat-sync.service /etc/systemd/system/
  1163. $SERVICE_MANAGER enable stat-sync.service
  1164. print_info "stat-sync service enabled"
  1165. fi
  1166. # eye-statd service (NetFlow)
  1167. if [[ -f "/opt/Eye/docs/systemd/eye-statd.service" ]]; then
  1168. cp /opt/Eye/docs/systemd/eye-statd.service /etc/systemd/system/
  1169. $SERVICE_MANAGER enable eye-statd.service
  1170. print_info "eye-statd service (NetFlow) enabled"
  1171. fi
  1172. # Configure DHCP
  1173. setup_dhcp_server
  1174. # Configure syslog
  1175. setup_syslog
  1176. }
  1177. # Import MAC address database
  1178. import_mac_database() {
  1179. print_step "Importing MAC address database"
  1180. if [[ -f "/opt/Eye/scripts/utils/mac-oids/download-macs.sh" ]]; then
  1181. cd /opt/Eye/scripts/utils/mac-oids/
  1182. # Download MAC database
  1183. print_info "Downloading MAC address database..."
  1184. bash download-macs.sh
  1185. # Update vendors
  1186. if [[ -f "update-mac-vendors.pl" ]]; then
  1187. print_info "Updating vendor information..."
  1188. perl update-mac-vendors.pl
  1189. fi
  1190. cd - >/dev/null
  1191. else
  1192. print_warn "MAC address import scripts not found"
  1193. fi
  1194. }
  1195. # Final instructions
  1196. show_final_instructions() {
  1197. echo ""
  1198. echo -e "${GREEN}===========================================${NC}"
  1199. echo -e "${GREEN} INSTALLATION COMPLETED SUCCESSFULLY! ${NC}"
  1200. echo -e "${GREEN}===========================================${NC}"
  1201. echo ""
  1202. echo "SYSTEM INFORMATION:"
  1203. echo " Distribution: $OS_NAME"
  1204. echo " Version: $OS_VERSION"
  1205. echo " Database: $DB_TYPE"
  1206. echo " Language: $EYE_LANG" # <-- Добавлено
  1207. echo " User: eye"
  1208. echo " Directory: /opt/Eye"
  1209. echo ""
  1210. echo ""
  1211. echo "TO COMPLETE SETUP, EXECUTE:"
  1212. echo ""
  1213. echo "1. Configure database security:"
  1214. if [[ "$DB_TYPE" == "postgresql" ]]; then
  1215. print_info " PostgreSQL: Edit pg_hba.conf if needed"
  1216. if [[ -f "/root/eye_postgres_password.txt" ]]; then
  1217. echo ""
  1218. echo "3. PostgreSQL 'stat' user password saved in:"
  1219. echo " /root/eye_postgres_password.txt"
  1220. echo ""
  1221. fi
  1222. else
  1223. echo " mysql_secure_installation"
  1224. if [[ -f "/root/eye_mysql_password.txt" ]]; then
  1225. echo ""
  1226. echo "3. MySQL 'stat' user password saved in:"
  1227. echo " /root/eye_mysql_password.txt"
  1228. echo ""
  1229. fi
  1230. fi
  1231. echo ""
  1232. echo "2. Check and edit configuration files:"
  1233. echo " /opt/Eye/html/cfg/config.php"
  1234. echo " /opt/Eye/scripts/cfg/config"
  1235. echo ""
  1236. echo "4. Configure cron jobs:"
  1237. echo " nano /etc/cron.d/eye"
  1238. echo " Uncomment required scripts"
  1239. echo ""
  1240. echo "5. Configure if necessary:"
  1241. echo " - DHCP: /etc/dnsmasq.conf"
  1242. echo " - NetFlow: configure on network devices"
  1243. echo ""
  1244. echo "6. WEB INTERFACE ACCESS:"
  1245. echo " URL: http://$(hostname -f)/"
  1246. echo " Admin: http://$(hostname -f)/admin/"
  1247. echo " Login: admin"
  1248. echo " Password: admin"
  1249. echo ""
  1250. echo -e "${RED}IMPORTANT:${NC}"
  1251. echo " - CHANGE admin password and API key!"
  1252. echo " - Configure users and networks in web interface"
  1253. echo ""
  1254. echo -e "${GREEN}===========================================${NC}"
  1255. echo ""
  1256. }
  1257. # Final instructions
  1258. show_final_upgrade() {
  1259. echo ""
  1260. echo -e "${GREEN}===========================================${NC}"
  1261. echo -e "${GREEN} UPGRADE COMPLETED SUCCESSFULLY! ${NC}"
  1262. echo -e "${GREEN}===========================================${NC}"
  1263. echo ""
  1264. }
  1265. # Install function
  1266. eye_install() {
  1267. clear
  1268. echo -e "${GREEN}===========================================${NC}"
  1269. echo -e "${GREEN} Installing Eye Monitoring System ${NC}"
  1270. echo -e "${GREEN} for ALT Linux/Debian/Ubuntu ${NC}"
  1271. echo -e "${GREEN}===========================================${NC}"
  1272. echo ""
  1273. # Глобальные переменные
  1274. MYSQL_PASSWORD=""
  1275. POSTGRES_PASSWORD=""
  1276. DB_TYPE="mysql"
  1277. EYE_LANG="english"
  1278. EYE_LANG_SHORT="en"
  1279. SQL_DATA_FILE=
  1280. SQL_CREATE_FILE=
  1281. INSTALL_TYPE="full"
  1282. DB_INSTALL="local"
  1283. # Execute installation steps
  1284. check_root
  1285. detect_distro
  1286. select_language_with_auto
  1287. select_database_type
  1288. update_system
  1289. install_packages
  1290. create_user_group
  1291. install_source_code
  1292. download_additional_scripts
  1293. setup_database
  1294. setup_configs
  1295. apply_language_settings
  1296. setup_apache_php
  1297. setup_cron_logrotate
  1298. setup_additional_services
  1299. import_mac_database
  1300. show_final_instructions
  1301. }
  1302. # Upgrade function
  1303. eye_upgrade() {
  1304. clear
  1305. echo -e "${GREEN}===========================================${NC}"
  1306. echo -e "${GREEN} Update Eye Monitoring System ${NC}"
  1307. echo -e "${GREEN}===========================================${NC}"
  1308. echo ""
  1309. check_root
  1310. detect_distro
  1311. update_system
  1312. install_packages
  1313. install_source_code
  1314. import_mac_database
  1315. show_final_upgrade
  1316. }
  1317. # Function to display help
  1318. show_help() {
  1319. echo "Usage: $0 [options]"
  1320. echo ""
  1321. echo "Options:"
  1322. echo " --help, -h Show this help"
  1323. echo " --upgrade, -u Automatic upgrade"
  1324. echo " --install, -i Interactive install"
  1325. echo ""
  1326. echo "Supported distributions:"
  1327. echo " - ALT Linux 11.1+"
  1328. echo " - Debian 11+"
  1329. echo " - Ubuntu 20.04+"
  1330. echo ""
  1331. }
  1332. # Function to check user existence
  1333. check_user() {
  1334. id "eye" &>/dev/null
  1335. return $?
  1336. }
  1337. # Function to check directory existence
  1338. check_directory() {
  1339. [ -d "/opt/Eye" ]
  1340. return $?
  1341. }
  1342. # Handle command line arguments
  1343. case "$1" in
  1344. --help|-h)
  1345. show_help
  1346. exit 0
  1347. ;;
  1348. --upgrade|-u)
  1349. mode="upgrade"
  1350. echo "Mode set to: upgrade"
  1351. ;;
  1352. --install|-i)
  1353. mode="install"
  1354. echo "Mode set to: install"
  1355. ;;
  1356. *)
  1357. # autodetect mode
  1358. echo "Auto-detecting installation status..."
  1359. if check_user; then
  1360. user_exists=true
  1361. echo "✓ User 'eye' exists"
  1362. else
  1363. user_exists=false
  1364. echo "✗ User 'eye' does not exist"
  1365. fi
  1366. if check_directory; then
  1367. dir_exists=true
  1368. echo "✓ Directory /opt/Eye exists"
  1369. else
  1370. dir_exists=false
  1371. echo "✗ Directory /opt/Eye does not exist"
  1372. fi
  1373. if $user_exists && $dir_exists; then
  1374. mode="upgrade"
  1375. echo "Existing installation detected. Switching to upgrade mode."
  1376. else
  1377. mode="install"
  1378. echo "No existing installation found. Switching to install mode."
  1379. fi
  1380. ;;
  1381. esac
  1382. echo ""
  1383. echo "Selected mode: $mode"
  1384. # Main execution based on mode
  1385. case "$mode" in
  1386. "upgrade")
  1387. echo "Starting upgrade process..."
  1388. # Start upgrade
  1389. eye_upgrade
  1390. ;;
  1391. "install")
  1392. echo "Starting installation process..."
  1393. # Start installation
  1394. eye_install
  1395. ;;
  1396. *)
  1397. echo "Error: Unknown mode '$mode'"
  1398. exit 1
  1399. ;;
  1400. esac
  1401. # Exit with success code
  1402. exit 0