install-eye.sh 50 KB

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